Hello, i have a problem with GameObject.Find() function.
I’m working on a strategy game, so i have a game object worldmap which contains a list of countries (game object too) and each country contains a list of game objects (for each area) and finaly each area have 2 game objects.
I use GameObject.Find() to select one of the 2 last game objects by his name (they have unique name).
In the start function, I want to select a few numbers of these game objects.
My problem is that GameObject.Find() returns null for some game objects.
When i tried to find why, i did this :
First, GameObject.Find() returns me one of the selected gameObjects, let’s call it “testname” (all letters are in lower case)
I changed the last letter (testnamo) => GameObject.Find(“testnamo”) returns me again the gameObject
I renamed the gameObject in “testname” (his initial name) => GameObject.Find(“testname”) didn’t find anyway the gameobject
I renamed it “testnamo” => GameObject.Find(“testnamo”) works this time
I renamed it “testnamo2” => GameObject.Find(“testnamo2”) returns null …
I renamed it “testnamot” => GameObject.Find(“testnamot”) returns null …
I renamed it “testnamo” => GameObject.Find(“testnamo”) find it (and it seems to be the only name that he can find for this area …)
I just don’t understand why the result of GameObject.Find() can depends of the name …
I made no mistake in my script (i copy/paste the name), all the name tested are unique and all objects were created as game object.
I am really not sure what you’re asking. You should probably post the part of your script that’s causing the issue, because the Find method should be secure.
Yeah, since it’s supposed to find an object by name, of course it depends on the name?
I am not at home now so I can’t show you code for the moment but the point is that GameObjet.find method don’t find the object if I rename it (of course I changed the name too in the code).
Example :
The name of my game object is “test”. GameObjet.find(“test”) find the object.
I rename the object “tesa”. GameObjet.find(“tesa”) return null .
It’s what I was talking about in my first message.
I edited my first mesage. I hope this is more clearly.
Well, I have never come across that problem. Just to get the obvious out of the way - you are stopping the game and recompiling the code after changing it, right?
Is there a way that you could throw together a quick scene that has the problem? I actually never heard of this before.
Yes.
I ran the game, looked at the console to see if Debug.Log returns any result. So i saw that first, it found 3 of the 9 game objects I wanted.
I stopped the game, modified the game object’s name in the hierarchy (and saved it), and then in the script (and saved it).
I ran again the game, and saw that it found only 2 of the 9 game objects.
I will try to give more details tonight if i have time