Search Unity

OnNetworkInstantiate ... finding out the right foundation

Discussion in 'Multiplayer' started by dacloo, Nov 1, 2007.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    I use Network.Instantiate. That works. But then I want to disable or enable components of this created prefab, based on wheter its locally instantiated or remotely.

    Code (csharp):
    1. function OnNetworkInstantiate (info : NetworkMessageInfo) {
    2.     Debug.Log("New object instantiated by " + info.sender);
    3.  
    4.    if (!info.NetworkViewID.isMine) {
    5.         // euh..I mean "if the instantiated object isn't local
    6.      //clone.GetComponent(CharacterController).enabled=false;
    7.         //clone.GetComponent(char_move).enabled=false;
    8.         }
    9.          
    10. }
    11.  
    How can I access "clone" in the function above? Where is this prefab that has been instantiated on the Network.

    Basicly I am not sure how to get to the actual prefab on the individual client
    and say "disable walking, coz you're a remote object"

    This OnNetworkInstantiate event is executed on the client, right?

    Thanks!
     
  2. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
    OnNetworkInstantiate() runs on all machines, server and clients.

    To check for ownership you could do:

    Code (csharp):
    1.  
    2. function OnNetworkInstantiate (msg : NetworkMessageInfo) {
    3.     if (networkView.isMine)
    4.     {
    5.         // Enable stuff for local player
    6.     }
    7.     else
    8.     {
    9.         // Disable stuff for remote player
    10.     }
    11. }
    12.  
    If this script is attached to the instantiated object (the prefab) it will do what you need.
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi Larus...Thank you! Oh so simple solution! Thanks for the last line, coz it wasn't clear to me where to put the script.
     
  4. DrHotbunz

    DrHotbunz

    Joined:
    Feb 14, 2009
    Posts:
    315
    And thank you again 2 years later for the info. Still works.