- Home /
need to capture all the object instantiated to the multiplayer
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
Answer by winsjansen · Apr 07, 2015 at 09:13 AM
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.
thanks,
i tried to change the name of the find to ship(clone) same as the inspector. still i get null.
i knew that i will probably have a problem with the name so i set a tag to gameObject
but the result is the same
Hm, I don't know what you are doing wrong.
Can you try instantiating 5 objects and tell me the name of each one of them, also check what tag each of them carry?
every one of the objects is called Ship(Clone) the tag name is Ships I entered the tag this way
gameObject.tag = "Ships";
I don't get why you don't use a game object variable to instantiate your prefab and then add it directly to a List. Then, you can browse the elements of the list by using a for loop with i < list.Count - 1. This way you have instant access to every ship in your scene. The list should be List .
Problem is you can't just cast the method from the API as far as I know, since it doesn't return anything. But as I already mention to him you should try casting it and see;
GameObject name = (GameObject)photonNetwork.instantiate();
Answer by wesleywh · Apr 07, 2015 at 07:18 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192425im_/https://answers.unity.com/themes/thub/images/avi.jpg)