Instantiated projectile spawns with viewID 0, causes errors

Options
I'm instantiating a bottle rocket prefab in my scene and am trying to destroy it and spawn a particle system explosion when it collides with something. The rocket and explosion spawn, but the rocket doesn't get destroyed and I get these errors:
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:18)

(Warning)
Had to lookup view that wasn't in photonViewList: View (0)0 on BottleRocketProjectile(Clone) (scene)
UnityEngine.Debug:LogWarning(Object)
NetworkingPeer:GetPhotonView(Int32) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2680)
NetworkingPeer:ExecuteRPC(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2010)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2955)
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:18)

(Error)
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:19)

(Warning)
Had to lookup view that wasn't in photonViewList: View (0)0 on BottleRocketSplat(Clone) (scene)
UnityEngine.Debug:LogWarning(Object)
NetworkingPeer:GetPhotonView(Int32) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2680)
NetworkingPeer:ExecuteRPC(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2010)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2955)
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:19)

(Error)
PhotonView with ID 0 has no method "DestroyOnNetwork" marked with the [RPC](C#) or @RPC(JS) property! Args: Int32
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:2955)
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:19)

The objects do get instantiated with a viewID of 0, not sure if that's indicative of something wrong, but the error message makes it seem so.

This is the code I use to instantiate the rocket
void fireRocket(Vector3 bottleRocketStart, Quaternion bottleRocketRot)
    {
  
        GetComponent<PhotonView>().RPC("fireRocket_RPC", PhotonTargets.All, transform.position, Quaternion.identity, nm.teamID);
        currentItem = "";
    }

    [RPC]
    void fireRocket_RPC(Vector3 bottleRocketStart, Quaternion bottleRocketRot, int teamID)
    {
        //bottleRocket = (GameObject)PhotonNetwork.Instantiate("BottleRocket", bottleRocketStart, bottleRocketRot, teamID);
        bottleRocket = (GameObject)Instantiate(Resources.Load("BottleRocketProjectile"), mouseWeaponOrigin.transform.position, Camera.main.transform.rotation);
        bottleRocket.GetComponent<Rigidbody>().AddForce(bottleRocket.transform.forward * rocketForce);
        bottleRocket.GetComponent<RocketInfo>().shotByPlayer = teamID;
        Debug.Log("After instantiation");
        // NullReferenceException here
        Debug.Log("Spawned bottle rocket position: " + bottleRocket);
    }

Here is the code I'm using to destroy the rocket and spawn the explosion
void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.name != "BottleRocketProjectile(Clone)" && col.gameObject.name != PhotonNetwork.playerName) { 
            if (col.gameObject.tag == "Cat")
            {
                // Kill current cat
                // Spawn player who fired projectile as cat at cat's house.
            }
            Debug.Log("Rocket collided with " + col.gameObject.name);
            GetComponent<PhotonView>().RPC("spawnExplosion", PhotonTargets.All);
            GetComponent<PhotonView>().RPC("DestroyOnNetwork", PhotonTargets.All, GetComponent<PhotonView>().viewID);
        }
    }

    [RPC]
    void spawnExplosion()
    {
        GameObject bottleRocketSplat = (GameObject)Instantiate(Resources.Load("BottleRocketSplat"), transform.position, transform.rotation);
    }

    [RPC]
    public void DestroyOnNetwork(int pvID)
    {
        PhotonNetwork.Destroy(PhotonView.Find(pvID));
    }

I'm using Unity 5.0.2f1

I've looked at the instantiated objects in the inspector during runtime and they are spawned with a view ID of 0. Is there something else I need to do in the instantiation lines?

Comments

  • vadim
    Options
    Hi,

    Use PhotonNetwork.Instantiate to create objects for which you call GetComponent<PhotonView>().RPC() or PhotonNetwork.Destroy
    For such objects, make sure that you create them only on one of room's client. Instances on other clients will be created automatically.
  • carnivoris
    Options
    OK, so, now I'm brought back to an issue I had yesterday detailed here

    viewtopic.php?f=17&t=6185&p=23612#p23612

    If I use PhotonNetwork.Instantiate, nothing happens. Nothing gets spawned, no errors, no warnings, nothing.
    void fireRocket(Vector3 bottleRocketStart, Quaternion bottleRocketRot)
        {
      
            GetComponent&lt;PhotonView&gt;().RPC("fireRocket_RPC", PhotonTargets.MasterClient, transform.position, Quaternion.identity, nm.teamID);
            GetComponent&lt;GetItem&gt;().currentItem = "";
        }
    
        &#91;RPC&#93;
        void fireRocket_RPC(Vector3 bottleRocketStart, Quaternion bottleRocketRot, int teamID)
        {
            bottleRocket = (GameObject)PhotonNetwork.Instantiate("BottleRocketProjectile", bottleRocketStart, bottleRocketRot, teamID);
            //bottleRocket = (GameObject)Instantiate(Resources.Load("BottleRocketProjectile"), mouseWeaponOrigin.transform.position, Camera.main.transform.rotation);
            //bottleRocket.GetComponent&lt;Rigidbody&gt;().AddForce(bottleRocket.transform.forward * rocketForce);
            //bottleRocket.GetComponent&lt;RocketInfo&gt;().shotByPlayer = teamID;
            Debug.Log("After instantiation");
            // NullReferenceException here
            Debug.Log("Spawned bottle rocket position: " + bottleRocket);
        }
    

    I was talking with Tobias in another thread yesterday and he suggested that I not network instantiate projectiles but I clearly don't understand how to do that correctly. I'm very new to network programming and I'm just a bit lost. Can you help me out with some guidance here?

    EDIT: It seems as though the correct way of doing things is not placing a PhotonView on the projectile, but I don't understand how to call an RPC to destroy the projectile if there's no PhotonView. Anyone know how to deal with this?
  • Blocker226
    Options
    Projectiles spawned through PhotonNetwork.Instantiate can only be destroyed by the owner GameObject that creates them. If your projectiles are numerous, I suggest you follow what Tobias said and instead RPC to instantiate GameObjects through the default Unity method.

    For my Grenade Launcher projectiles, which can be viewed with this link:https://dl.dropboxusercontent.com/u/18297273/WebTest/WebTest.html

    They have their own script to destroy themselves after their 3 second fuse runs out and the explosion is spawned, through GameObject.Destroy. No PhotonView component needed, saves on FPS and network lag.

    What I'd do if I were you is to create and add normal collide-destroy scripts to the rockets, and use RPCs to instantiate them through the normal GameObject.Instantiate methods. PhotonNetwork.Instantiate should be reserved for more permanent GameObjects like players and vehicles.

    Sigh, if you do look at the demo, some of the grenades are known to disappear without exploding. Haven't found an answer for that though.
  • carnivoris
    Options
    Ohhhhh... alright, now I see that I've been thinking too hard about the networking. Alright, good deal... now I have to figure out why the hell I'm getting a NullReferenceException when I try to spawn the explosion :/ But, that's another issue. Thanks for your help!
  • Blocker226
    Options
    You're welcome, and all the best with your project! :)
  • NomadicWarrior
    Options
    Dropbox link is 404 error:(