- Home /
Referencing variables / functions on another script
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:
static var isGrounded : boolean = false;
function OnTriggerStay () {isGrounded = true;} function OnTriggerExit () {isGrounded = false;}
And this is a part of just another script where I wanted to use it:
function FixedUpdate () {
if (GroundCheck.isGrounded)
}
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.
Why does the box need both scripts? Is the box a player? I would have thought you would only need to check if the player is grounded, or do you wish to check if the box is grounded, independent of the player?
@$$anonymous$$otionreactor Well this is a general program$$anonymous$$g question, I was just giving my instance of usage. So there could be a better way to do what I'm trying, and I'd love to hear it. I wanted to separate this because I used GroundCheck first on the player for other things, and then when I did the second script it would use the GroundCheck, so if I want to use the second I need the first. In the end it's just a way of organizing. In this instance I could just remove the first script and copy the very small code into each place I need it. But what if I want to know if the other object "isGrounded"?
Answer by Ashkan_gc · Feb 26, 2010 at 05:34 AM
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.
That's great stuff to know! Now, what if I mix them all? Set a public variable then on Start() if it's null search for GetComponentsInChildren and if it can't find it then use GameObject.Find - How about that?
it's a good approach because if you or your level designer forget to attach the correct component it will correct it. but it's good to use just one of the second or third methods. if the component is always in your children use the second method but if sometimes it's there and sometimes in other gameObjects then use the third method. it really depends on your project. if %80 of the times it's in your children, what you wrote is the best approach.
Just a note: can't use GameObject.Find directly, need to do .GetComponent. Now I'm just trying to make a static function for using on the Start then I'll post it here... As soon as I can get around the issues I'm having with it.