Hi guys, Im creating a multiplayer game, in it Im instatinating a ship using
photonNetwork.instantiate();
the instantiate is working fine
what i need to do next is to find all the gameobjects created in this way and to enter it to array or a list
i tried to use
GameObject.Find(ship);
or even
GameObject.findGameObjectsWithTag(ships);
both returning null
I don’t have any experience with photonNetwork api, but I’m pretty sure that when you instantiate an object it will always be given a unique name in some way, for example “ship1” or “ship clone v2” etc, therefore you can’t just search for “ship” as it doesn’t exists (try check the inspector in the editor on runtime to see the name of the objects)
I suggest you try to cast the instantiation as a transform, and set it’s parent to another static gameobject so that you can just iterate through it, to create a list of all it’s childs like this;
foreach (Transform child in theObjectYouAddEveryShipTo)
{
List<GameObject> listOfThemAll = new List<GameObject>();
listOfThemAll.Add(child);
}
I’m not sure where you would want to cast it though, as I’ve not used photonNetwork before, but if you need any more help please let me know, and we’ll try to sort it for you.
If all you want to do is add it to some array list on a object just make a script that executes when the object is instantiated. On that script have it access some public variable on another object and have it push it onto its array. That’s the easiest thing I can think of doing.
As it was mentioned before you can’t search by names because they are going to change. You will have to search by tags by you typed it wrong(according to what you posted).
GameObject myObject[] = GameObject.FindGameObjectsWithTag("Some Tag");//idk if i typed the array part right so just double check this.
You typed it with a lower case find. However, you will have to make sure not to add it twice thats why just having a OnStart() script would be the easiest thing to do.
Just put that OnStart script onto the objects prefab.