• 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 /
avatar image
Question by carnivoris · Jun 10, 2015 at 03:22 PM · networkinginstantiatephotonrpcprojectile

Proper way to instantiate projectile in PUN

I've been fighting with Photon Unity Networking for a few days now with trying to instantiate my rocket projectile properly. I've been told by Tobias on the Exit Games forums that using PhotonNetwork.Instantiate is completely unnecessary (good t$$anonymous$$ng, too... because it didn't do anyt$$anonymous$$ng) and that it's best to just use an RPC call. I've tried that with t$$anonymous$$s code:

     void fireRocket(Vector3 bottleRocketStart, Quaternion bottleRocketRot)
     {
   
         GetComponent<PhotonView>().RPC("fireRocket_RPC", PhotonTargets.MasterClient, transform.position, Quaternion.identity, nm.teamID);
         GetComponent<GetItem>().currentItem = "";
     }
 
     [RPC]
     void fireRocket_RPC(Vector3 bottleRocketStart, Quaternion bottleRocketRot, int teamID)
     {
 
         bottleRocket = (GameObject)Instantiate(Resources.Load("BottleRocketProjectile"), mouseWeaponOrigin.transform.position, Camera.main.transform.rotation);
         bottleRocket.GetComponent<RocketInfo>().shotByPlayer = teamID;
     }

T$$anonymous$$s successfully spawns the rocket. However, I run into issues when trying to destroy the rocket in t$$anonymous$$s script, w$$anonymous$$ch is attached to the rocket prefab.

     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.name != "BottleRocketProjectile(Clone)" && col.gameObject.name != PhotonNetwork.playerName) { 

             GetComponent<PhotonView>().RPC("spawnExplosion", PhotonTargets.MasterClient);
             GetComponent<PhotonView>().RPC("DestroyOnNetwork", PhotonTargets.MasterClient);
         }
     }
 
     [RPC]
     void spawnExplosion()
     {
         GameObject bottleRocketSplat = (GameObject)Instantiate(Resources.Load("BottleRocketSplat"), transform.position, transform.rotation);

     }
 
     [RPC]
     public void DestroyOnNetwork()
     {

         Destroy(gameObject);
     }

The explosion is spawned, but the rocket is not destroyed. I get errors here complaining about viewIDs... w$$anonymous$$ch makes sense, since I'm not PhotonNetwork.Instantiating them. That's where I'm losing track of what I'm supposed to be doing. If I'm not supposed to instantiate them on the network, how do I properly call the RPCs to handle the destruction of the rocket and instantiation of the explosion?

These are the errors I get on collision with somet$$anonymous$$ng

 Illegal view ID:0 method: spawnExplosion GO:BottleRocketProjectile(Clone)
 UnityEngine.Debug:LogError(Object)
 NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2915)
 PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
 PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
 PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
 RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:24)
 

 Illegal view ID:0 method: DestroyOnNetwork GO:BottleRocketProjectile(Clone)
 UnityEngine.Debug:LogError(Object)
 NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2915)
 PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
 PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
 PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
 RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:25)
 

 PhotonView with ID 0 has no method "DestroyOnNetwork" marked with the [RPC](C#) or @RPC(JS) property! Args: 
 UnityEngine.Debug:LogError(Object)
 NetworkingPeer:ExecuteRPC(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2190)
 NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2979)
 PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
 PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
 PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
 RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:25)
 
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by 334499p · Jul 19, 2015 at 01:23 AM

Since the rocket is technically client-based, you would need to destroy it on all clients, not just the Master Client. If the rocket were instantiated using the photon network then you would need to use PhotonNetwork.Destroy(PhotonView), but t$$anonymous$$s isn't the case.

-Errors: -Only instantiating the rocket on the master clients... you need to call PhotonTargets.All instead of PhotonTargets.MasterClient for the first script and make sure there is a PhotonView attached to the object that contains that script.

-Calling an RPC for the explosion of the rocket... You don't need to call an RPC since all rockets are client based. Say you have 10 players on the server and one person shoots a rocket; that means that the one person who shoots the rocket tells all players including themselves through an RPC to spawn a rocket that isn't actually synced across the network. When each client spawns t$$anonymous$$s rocket it will follow its own script, so you need to only have the OnCollisionEnter and the proceeding explosion and Destroy () method for the second script with no PhotonView attached the rocket prefab.

Using t$$anonymous$$s method however is not ideal if you actually want to tie operations to the explosion of the rocket such as hurting other players. A different method would be to use two different rockets and their own scripts. One rocket would belong to the player(and could only be seen by that player) and would cover all operations such as damage.The other rocket prefab would have it's own script to simply explode on collision and not$$anonymous$$ng else for the other clients. One RPC would execute the spawning of projectiles for PhotonTargets.Others and then you would spawn you're own rocket. Neither rocket prefab would need a PhotonView or RPCs to die as mentioned above.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Photon Network Instantiate Objects over Network 1 Answer

Synchronize array with simultaneous access via Photon Network RPC 1 Answer

More efficient way to call these RPC's (Code works) 1 Answer

UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer

How to assign Photon Instantiated object's public functions to a gui button events such as OnPointerEnter 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