the problem: Unity only finds one object tagged with “Legs”. The second position do not exist…both objects are tagged correctly. So I don’t know what is the problem…
Am I using wrong the FindGameObjectsWithTag method?
The greater problem here is a flawed design assumption. You are assuming there is going to be some consistency in the order in which Unity returns the GameObjects. The documents don’t specify any specific order so you should not rely on it. Changes between Unity versions or Webplayer updates could change the internal logic of how it finds tagged objects and your code will break.
If you don’t care which is active, you can simply call FindWithTag() and it will return one of them. Other than that, I think you need to take a step back and rethink your design.
I noticed that the documentation and the method do not have a specific order to return the objects.
But that wasn’t a big problem for me, since I was just testing some methods to use in my next scene. The problem were that I could not understand why the FindGameObjectsWithTag were returning only one of the 2 objects with the tag.
But thank you very much for the reply! I’ll check another methods and paths to do my game! =)
Is one of the legs already deactive? According to the documentation, FindGameObjectsWithTag only returns active objects. So if one of your legs is already deactive, it won’t be returned in that array.
If your legs and the script are already in the scene, or part of the same prefab, I would use this.
// Assign in inspector
public GameObject[] legs;
// ...
public void setLegsActive(int whichLegs,bool value) {
legs[whichLegs].SetActive(value);
}
I think Garth has the right solution: one leg is probably already deactivated. I’m just concerned that setLegsActive(0, true) won’t always returned the same object when passed 0 so you’re going to get unpredictable behaviour. And that may or may not be a problem.