Search Unity

Get or Find object with network identity over the network

Discussion in 'Multiplayer' started by leizzer, May 20, 2016.

  1. leizzer

    leizzer

    Joined:
    Jun 26, 2013
    Posts:
    39
    Hello guys,

    I'm writing this because I can't find a clear response to my problem.

    I want to instantiate object on the server and ignore collision with the owner of that object. So far sounds easy.

    The problem for me is that I have an object (with network identity) that its purpose is to create the correct kind of object, lets call it BulletCreator. The client will ask the BulletCreator what kind of bullet must create and the client also tells what player owns it (so the collider can ignores him). This creation request will be via Command and the server should response to the player with bullet-object's network id or something so the player can refers to it locally.

    The questions are:
    1- How can I send to the server my playerControllerId and find the player on the Server side?
    2- How to send back to the player (because of multiple player on client) a reference to the object spawned on the server? (Sending a message probably but with what data and how to find the object on the client).

    Thanks for your help and ask if I should explain myself better.

    Cheers
     
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    1. You can get the ID in the Networkbehaviour script like this: NetworkInstanceId playerId = this.netId;
    You can send it then via a command to the server and there you can find the object via: NetworkServer.FindLocalObject(playerId)

    Hope that helps a bit.
     
  3. leizzer

    leizzer

    Joined:
    Jun 26, 2013
    Posts:
    39
    Thank you, that helped me to close the gap to my goal :)
    I tried it and works fine.
     
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Also, while you can't use FindLocalObject() on the client, you can have the server send the netId of the spawned object to clients and just use FindObjectsOfType(NetworkIdentity) and search for the one that matches the netId.
     
  5. JirinCrawford

    JirinCrawford

    Joined:
    May 2, 2016
    Posts:
    5
    You can run ClientScene.FindLocalObject instead of NetworkServer.FindLocalObject on the client (just make sure to pick the right function if you're in a bit of code which runs on both).
     
    TwinCrows likes this.
  6. leizzer

    leizzer

    Joined:
    Jun 26, 2013
    Posts:
    39
    If I don't misunderstand you. Your option is to find all the objects of type NetworkIdentity on the client and then search (iterate) for the matching netId.

    But I found this other way:

    Code (csharp):
    1. ClientScene.FindLocalObject(theNetworkId)
    And this close my circle :)

    Edit: JirinCrawford posted the same while I was replying
     
    gwelkind likes this.
  7. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Nice! I was not aware of that function :)
     
  8. brainwipe

    brainwipe

    Joined:
    Aug 21, 2017
    Posts:
    78
    For anyone arriving here using Mirror in (checks clock) 2021 then...

    Code (CSharp):
    1. ClientScene.FindLocalObject(theNetworkId) // DEPRECATED
    2.  
    3. NetworkIdentity.spawned[theNetworkId] // USE THIS
     
  9. lamoos

    lamoos

    Joined:
    Jan 23, 2015
    Posts:
    2
    Any idea why Why client scene does not contain a definition for FindLocalObject for me?

    Edit:
    Use this now guys:
    NetworkIdentity.spawned[netId].gameObject;
     
  10. BetaSchrodinger

    BetaSchrodinger

    Joined:
    Apr 21, 2018
    Posts:
    2
    Much Thanks!
     
    brainwipe likes this.
  11. greysun

    greysun

    Joined:
    Feb 23, 2017
    Posts:
    9
    Code (CSharp):
    1. NetworkServer.spawned[netId] //current as of 5-2022
     
    NikolayHadzhiev likes this.
  12. TillRossberg

    TillRossberg

    Joined:
    Dec 3, 2019
    Posts:
    1
    update: NetworkManager.SpawnManager.SpawnedObjects[objectID];
     
    diegoop and RedwireInteractive like this.