- Home /
Network.Instantiate instantiates twice
Hi,
I'm working on a gun for a multiplayer. When I spawn a projectile the guns spawns it twice this should be just one time. I have a weapon control script:
public void Fire (int myID) {
foreach (Weapon wp in weapons) {
wp.Fire (myID);
Debug.Log (myID);
}
}
a weapon script:
public void Fire (int myID) {
GetComponent<NetworkView>().RPC ("Shoot", RPCMode.AllBuffered, myID);
}
[RPC]
public void Shoot (int myID) {
GameObject thisProjectile;
thisProjectile = (GameObject) Network.Instantiate (projectile, transform.parent.position + offSet, transform.parent.rotation, 2);
thisProjectile.GetComponent<Projectile>().setProjectile(myID, speed);
}
and a projectile script:
void Update () { //A quick fix for now
if (myOwner == 0)
Destroy (gameObject);
}
public void setProjectile (int owner, Vector3 speed) {
myOwner = owner;
myRigidbody = gameObject.GetComponent<Rigidbody>();
myRigidbody.AddForce (speed);
}
there is just one weapon script attached to my player and his children, both the projectiles spawn at the same spot but one doesn't get assigned an ID and stays 0. I've placed a Debug.Log in every void in the chain and nothing get's called twice. For now I can just destroy the projectiles with the owner 0. but it annoys me that it is spawning twice and I can't figure out why it does that.
put some debug statements in your code and figure out which method gets called twice - Fire in weapon control, Fire in weapon (doesn't that get confusing? I'd give them slightly different names), or Shoot.
Well, I renamed the functions in weapon wpShoot and wpFire. I already tried a debug on every void but I tried it again. Oh I should say this only happens when two players are connected. If I shoot from within the editor I get a setProjectile, wpShoot, wpFire, Fire and the destroyed (extra) game object all called once. If I shoot in the build and check the debug in the editor I don't get wpFire and Fire but the rest is all called just once as well.
Answer by toddisarockstar · Mar 13, 2015 at 06:21 AM
Instantiate does not go inside the RPC Call!!! saying Network.Instantiate is enough. you can state it elsewhere such as update or start. it automatically creates the objects on all the connected machines. no rpc nessisarry.
so looks like your script is sending a rpc to every player. and now every player in your game creates his own copy of bullets for every player !!!!!!!
another words, if your game was 4 player, each player would receive 16. interesting concept but I don't think that's what you want!
Thanks, this works with PhotonNetwork.Instantiate as well. That's why I was getting these double spawns!
Answer by Addyarb · Mar 13, 2015 at 07:22 AM
If you're calling instantiate within an RPC, you just use "Instantiate" instead of "Network.Instantiate." RPC functions essentially do the same exact thing as Network functions, they are just more customized because you can edit the function. However, depending on what type of server you're running (Authoritative or non-authoritative) you'll want to use different RPC modes and such.