- Home /
all's well. my problem is no longer problematic, if you catch my drift
Find + GetComponent not working as expected
I have a script that should make player two's (named paddle2 in the scene) 'hooped' public variable true.
function OnTriggerEnter(){
GameObject.Find("paddle2").GetComponent("randomeffects").hooped = true; // player two's name is paddle2
}
this works great, until i throw in another player in the scene called 'paddle1', then it sometimes changes paddle1's hooped variable to true instead.
paddle1 and paddle2 have the same scripts on them the only thing different is they're names.
why does this happen? How can i make it not happen?
thank you for your time
have a Quishtay™ day!
Is it really the same script on both? I wouldn't think so since the other script probably says GameObject.Find("paddle1") right?
no, sorry, misleading. the script above is on a hoop, not the paddles. the script running the code above is not on either of the paddles and it is not called randomeffects. the hoop script is supposed to access the other scripts
The GameObject.Find("paddle2") will only ever return the game object with name paddle2 or null. There could be an error in your scene if you forgot to rename paddle1 or ins$$anonymous$$d set the tag ins$$anonymous$$d of the name. $$anonymous$$oreover, if the paddle is not found, you will get a null reference exception when you try to get the component off of a null object.
There isn't a bug in Unity for this, but if you want to be absolutely sure which object is being referenced, try this. $$anonymous$$ake a "public GameObject paddle" variable in your script. Place that script on the objects you like, and, in the inspector, drag the correct paddle from the scene onto the paddle. If you don't want to do that use an Awake function to find the game object, ins$$anonymous$$d of finding it each time the trigger function is called. You can see all of these best practices here http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
The other suggestion I have is to use the angle bracket notation ins$$anonymous$$d of string notation. It is faster than string lookup.
awwww. just figured out what i was doing wrong.
$$anonymous$$y variable in the other script was a static variable. I cant believe I did that.
thank you for you help anyway
although i would really like to know what is should use ins$$anonymous$$d of go.find
@rednax20 well, there is a way to set an object reference in the inspector. That will set the reference before the game ever starts. Go ahead an close the question or post your own answer and select it.
I think you shouldn't use go.find first of all for such things, in fact, if you can, you shouldn't use it at all.
You should really be setting the variable in the inspector so that it never has to do a find. If you do use a find, only do it in awake, or somewhere where it only needs to run once.
Follow this Question
Related Questions
Only change a variable on the instaniated object not the prefab. 0 Answers
How to spawn a GameObject by name? 2 Answers
Limiting respawns on scene 1 Answer
Cannot assign Public GameObject variable in Inspector... 2 Answers
How to set a public variable to a gameObject in the inspector through coding 1 Answer