I have 3 objects.
The player contains a child which is just a trigger. I've set GroundCheck to it.
The third is a separate object, a box.
Player and box are rigidbodies, the player child is not.
This is my full GroundCheck.js script:
And this is a part of just another script where I wanted to use it:
This was done following instructions on the documentation, and it is working, but not as expected. If I set both scripts just on the player, that's fine. If set both also in the box, when I check for GroundCheck it seems like the "isGrounded" variable is a global one that's just modified by each instance and the final behavior is quite odd from what I hoped.
It's pretty obvious the "another script" isn't specific calling the GroundCheck that I set to a child object or even in the same object - you can see that on the above code. Then how would I do it properly? I thought maybe using static-functions, or maybe using static class, but none of those actually answer my question.
I understand there are several other ways to reference and access other scripts or game objects, but I wonder which one would be best and which one would fit this specific situation.
There are two questions in one here. ;)
edit: I just found out I shouldn't be using "static" there... But I still don't know what should I be using instead.
edit2: here's a continuation to this issue.
for referencing other objects there are 3 ways. 1 make that object your child and use GetComponentsInChildren and ... 2 use a public variable and set the reference in inspector. this is a fast method but might mix you up in big projects. 3 use GameObject.Find in Start function to find what you want and store the result in a variable of class scope. each method has it's own usage. the first method is not good for referencing. the second method is good for verry general and reusable Scripts that many people want to use. the third way is the way that you should use for big projects and none reusable codes. when i say reusable i mean something verry general. watch the optimization video of unite 07 for more info.
answered Feb 26 '10 at 05:34 AM