Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Mubanga · Mar 12, 2015 at 11:49 PM · instantiatenetworknetwork instantiate

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siaran · Mar 13, 2015 at 12:02 AM 0
Share

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.

avatar image Mubanga · Mar 13, 2015 at 12:20 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Whiteleaf · Jan 18, 2016 at 12:50 AM 0
Share

Thanks, this works with PhotonNetwork.Instantiate as well. That's why I was getting these double spawns!

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Combination of Instance and random in a network game 2 Answers

Network Manager NetworkServer.Spawn isn't working 0 Answers

Do I need to instantiate multiplayer assets? 1 Answer

IgnoreCollision on a Network Instantiated Object 2 Answers

Please help with this code!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges