Search Unity

Network.instantiate and Netork.Destory.

Discussion in 'Scripting' started by rterranova, Mar 5, 2012.

  1. rterranova

    rterranova

    Joined:
    Nov 6, 2009
    Posts:
    43
    I am having issues with Network.Destroy. When I try to Destroy a Network instantiated game object it crashes Unity Editor. It also crashes the Webplayer if I build it.

    Here is what I do.

    var instance = Network.Instantiate(prim, pos, rot, 0);
    instance.networkView.RPC("SetOwnerInformation", RPCMode.All, LevelData.GetInstance().GetName());

    Then I do this to delete all objects of this type

    var prims : GameObject[];
    prims = GameObject.FindGameObjectsWithTag("PrimitivePrefab");

    for(prim in prims)
    {
    print("ShapeL: Netork View ID: " + prim.networkView.viewID);
    Network.RemoveRPCs(prim.networkView.viewID);
    Network.Destroy(prim.networkView.viewID);
    }

    The networkView.viewID match up. I have verified that. I do NOT get any more information from the Crash Report.

    Any ideas?
     
  2. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Do you actually declare the variable prim?

    ex:
    Code (csharp):
    1. var prim:GameObject
    If not do so.

    Or instead of
    Code (csharp):
    1. for(prim in prims)
    try
    Code (csharp):
    1. for(GameObject in prims)
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    Pretty sure that last line of code there just won't work. I know that UnityScript does magic, but how are you supposed to refer to the GameObject it gives you?

    Also, while it could be different in UnityScript, wouldn't it be "foreach" instead of "for"?
     
  4. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    In Unityscript, there is no foreach.
     
  5. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    You are right... I meant this:

    Code (csharp):
    1. for(var prim:GameObject in prims)
     
  6. rterranova

    rterranova

    Joined:
    Nov 6, 2009
    Posts:
    43
    Thank you for your suggestions. Unfortunately declaring "prim" as a GameObject does not fix this issue.

    What surprises me is that this crashes the Unity Editor.

    I have verified that the Network.viewIDs are correct.

    Any help would be greatly appreciated
     
  7. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Are you sure it is that script causing the crash? If that segment of code is apart of a larger code file, try commenting out certain chunks and trying again until the crash stops. Once you find the code chunk, start commenting out individual lines and trying to get rid of the crash.
     
  8. rterranova

    rterranova

    Joined:
    Nov 6, 2009
    Posts:
    43
    I have narrowed it down to this one line of code that crashes the Editor.

    But... I belive this is a side effect of the Network.Instantiate not working correctly. I'm currently trying to figure out why my objects are not being created correctly.
     
  9. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Try Network.Destroy(prim);
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you're already enumerating a list of GameObjects why not use the overload that takes a GameObject instead of a NetworkViewID?
     
  11. rterranova

    rterranova

    Joined:
    Nov 6, 2009
    Posts:
    43
    Ok, looks like the destroy may not be the problem.

    When a primitive is created using Network.Instantiate on the remote client I get this error on the local client.

    View ID AllocatedID: 15352 not found during lookup. Strange behaviour may occur
    (Filename: C:/BuildAgent/work/b0bcff80449a48aa/Runtime/Network/NetworkManager.cpp Line: 1676)
    Could't invoke RPC function 'SetOwnerInformation' because the networkView 'AllocatedID: 15352' doesn't exist

    I see this same issue from the Editor too.

    I'm not sure why I'm getting this message. I have two Avatars in the world at the same time and they are working correctly.