|
So I'm still not completely solid on how a GameObject is composed and how to access its various parts. I have a reference to a Collider, and need to cast it to the type of a script on the same GameObject, but I'm not sure what this would look like. My scenario looks like this (using C#):
Scripts:
And:
So how exactly do I perform that cast in the Enemy script? Thanks.
(comments are locked)
|
|
Unity uses a composition model - you can access the other components from a gameobject or component using GetComponent. In your case you want something like this: Thanks. I found the answer (posted in a comment on the question) a few minutes after posting this. Yay generics!
Dec 09 '10 at 07:55 PM
Ipsquiggle
Generics are fun! You can also use the non generic version if you're strange like that, Player p = (Player)other.GetComponent(typeof(Player));
Dec 09 '10 at 08:03 PM
Mike 3
(comments are locked)
|

Found this similar question which has the same answer: http://answers.unity3d.com/questions/17039/how-to-get-a-script-component-in-c