Any reason why this method of adding gameobjects with this tag to an array won't work?
Hi there, I'm trying to create an array with all the gameobjects in the scene that have a specific tag Previously I have used the same method with another project but for some reason in this project it doesn't seem to give me any results. Console is giving me no errors but pressing play I can see in the inspector that no gameobjects are being added to the array.
I have two scripts. One script is generating objects to the scene and giving them the tag The other script finds all the game objects with the tag and tries to place them in the array.
This is an example of the first script
public GameObject[] unowned;
// Use this for initialization
void Start () {
if (unownedPlanets == null) {
unowned = GameObject.FindGameObjectsWithTag("Unowned");
}
}
and here is the second line of code that I'm using to give the objects a tag. The tags are adding fine.
void Start () {
this.tag = "Unowned";
}
$$anonymous$$aybe you are instantiated both classes in the same frame? So maybe the first code runs before the second one, as result, it trying to find an objects while tag still not modified by the second code.
I tried placing the tag line in the awake function and keeping the other lines of code in the start function but still no luck. Found out it working in the update function so it must be as you suggested the order of instantiation. Is there a function that waits for the scene to load before executing?