Find a GameObject without a reference

Hello everyone,

I am building a RTS-Lockstep implementation right now and have a question:
I successfully instantiate gameObjects on all clients, but now I want to destroy one of them.
How do I identify a gameObject without a reference (because every client has another ref.)?
First thing I thought of is giving the gameObject a “public int ID” via script, but then I would have to iterate over all gameobjects to find the chosen one.`

There must be a better way…

GameObject LeObject = Network.Instantiate(“Prefab”);
int ID = NetworkView.ViewID;

Then you send that through and RPC or somefin…

GameObject LeObject = NetworkView.Find(ID).gameObject;

As side note: An RTS is a HUUUGEEEE pain to get going. I tell you this from experience. I recommend you try getting something like an FPS going or a game like Dungeon Crawlers before becoming this ambitious. Don’t lose hope though. Just learn the basics so you know what you’re doing. That way, you can code your game efficiently from the ground up and won’t have to redo it after realizing a fatal error.

Short Answer

Maintain all of your GameObjects in a List or Dictionary of some sort. Then call them by index, which can be your unique ID.

Long Answer

I do think you are getting lockstep wrong. Lockstep networking relies entirely on each client performing the simulation identically, based on a set of orders from all of the other clients. So the only data you should ever be passing through your network is the orders. When all orders are received each client will run the next step of the simulation.

So to lockstep an RTS you should never need to instantiate or destroy an object over the network. Rather you send the orders across all clients, and each client instantiates and destroys its own objects, based on a local calculation of the simulation.