network instantiating concept question

In my game, I have a game object with a script that spawns an enemy using network.Instantiate. This enemy has a network view and then spawns another enemy(which also has a network view), using network.Instantiate also. My problem was that the latter enemy was being spawned once for each player on the network, so instead of just spawning once, they would spawn twice if there was 2 players. I corrected this by putting the network.Instantiate code inside the if statement with the conditions networkview.isMine.

What is the difference between the two situations? I’ve been using network.instantiate with the other enemies without having to use that if statement and it works fine. Thanks

This is what’s going on:

“and then
spawns another enemy(which also has a
network view), using
network.Instantiate” [from inside a script on the first enemy]

The way you explained it, you are creating the first enemy using network.Instantiate from a SINGLE LOCATION [good]. This means that the first enemy is created only once across the network, and you will always get exactly the number you ask for - exactly one version of the same enemy on each client. The first machine tells every client to instantiate something.

However, you said that when that enemy spawns, it creates a sub-enemy from inside that enemy via Start() or Awake(), or some other function. The problem is that the function is called on EVERY CLIENT’s [bad] version of the enemy - one per client . For a normal calculation, such as making the enemy follow something,this is fine because the network will sync the data. The problem occurs when you try to create a new network object from each client independently. Each client will tell every other client to instantiate something.

by adding if(networkview.isMine), you are making it so that the sub-enemy is only created on ONE[good] machine . The machine then informs everyone about the sub-enemy he just made. This prevents replicated calls to network.Instantiate() from other clients. The first machine tells every client to instantiate something.

Hope this helped.