Heya!
I have a little doubt. I want to retrieve an instance of a Gameobject via its tag, but it doesn’t work. The weird thing is that it works if I try to retrieve it by its name.
This is the code that does not work:
public var Frog: GameObject[];
function Start(){
Frog = GameObject.FindGameObjectsWithTag("Player");
}
But this one does:
public var Frog: GameObject;
function Start(){
Frog = GameObject.Find("Frog(Clone)");
}
Obviously I’ve made sure that the Frog gameobject has the Player tag. What am I missing? Any ideas?
Despite this being an issue from 2016, I’ve re-encountered it today in 2021. Basically, when you’re instantiating an instance of a GameObject with tag “Player” in the Start() method and in another script you’re trying to use GameObject.FindGameObjectWithTag(“Player”); in it’s Start() method, then the two happen simultaneously or out of order where the player instance hasn’t spawned yet and you end up with a missing reference when your FindGameObjectWithTag() doesn’t see the player object in the level at that point in time.
The fix is to do a quick reference check during Update, FixedUpdate, or any other method that occurs when the level has been fully loaded.
Also OP, it looks like you have a typo in your Frog = GameObject.~ line of code. You used the method that returns an array of objects instead of just one, FindGameObjectsWithTag() vs FindGameObjectWithTag().
I hope this helps!
This is what i use to find a tag to check i it dose in fact have a tag. I know its not what ur looking for but it might help for testing in C#.
if (Frog.gameObject.tag == “Player”) {
Debug.Log(“Yup I Found IT!!!”);
}
else {
Debug.Log(“No luck this time”);
}