Reputation: 1723
I have a specific question here, but behind it is my confusion about data types and what Unity component names, tags and types really are.
The specific question is this: I want (at runtime, NOT via the editor) to be able to create components that can detect collisions and and pass the detection information on to other components. The components are quite simple - (invisible) cubes of various sizes with Colliders
attached, and isTrigger
set in each case. Then on collision detection (OnTriggerEntry()
, OnTriggerExit()
) to pass collision information on to other components, eg "hi, X has just collided with number 6".
Initially I wanted to be able to hook those calls on the assumption they were events - but it seems they are not events (MonoBehaviour
overrides, I assume). Reading elsewhere it appears the only way to do this is to attach the script (a C# class, in other words) dynamically after creation of the component.
Since there will be a number of these components, all with the same script (class) attached, I then need to set some internal class variables to tell it what to do / who to contact on collision. That is where the confusion starts.
It seems to me that the Unity designers / documenters, in an attempt not to burden people with too much code, tend to gloss over such details, or at least attempt to explain them in a non-software way. Understandable, but very confusing to someone like me who thinks in terms of software.
The class in the script I attach is going to have a name ('MyCollisionHandler
', for instance), but that is not an instance of a class, it is just the class definition - or type, if one prefers, eg 'class MyCollisionHandler { ...
'. However, it must be the case (unless it contains only static variables and functions) that once attached to the component, that there is an instance of it.
So, to put it simply - how do I get a reference to the specific instance of the general class type that is attached to that specific collision detecting object? Once I have that I can then use methods to set it up any way I like. If I use FindObjectOfType<MyCollisionHandler>()
, that isn't going to help because there might be loads of them and it will only return the first found. OK, I can use FindObjectsOfType<MyCollisionHandler>()
instead and get an array of them - but which is which?
So, it comes back to my general lack of understanding of Unity structure - the difference between component names, tags etc - and how they relate to class definitions and instances. To put it generally - how does one get to individual instances of class objects attached to Game Objects?
Upvotes: 1
Views: 658
Reputation: 4061
The class in the script I attach is going to have a name ('MyCollisionHandler', for instance), but that is not an instance of a class
It is, and the instance is returned by GameObject.AddComponent
.
MyCollisionHandler handler = gameObject.AddComponent<MyCollisionHandler>();
handler.someValue = 42;
Later you can get the instance with GameObject.GetComponent
, as you would do for any other component.
You would use FindObjectOfType
and FindObjectsOfType
if you don't have a reference to the gameobject the script/component is attached to, or you don't care what gameobject has this script/component attached and you just want one/all of them.
Upvotes: 2