Get or Find object with network identity over the network

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

  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.

Thank you, that helped me to close the gap to my goal :slight_smile:
I tried it and works fine.

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.

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).

1 Like

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:

ClientScene.FindLocalObject(theNetworkId)

And this close my circle :slight_smile:

Edit: JirinCrawford posted the same while I was replying

1 Like

Nice! I was not aware of that function :slight_smile:

For anyone arriving here using Mirror in (checks clock) 2021 then…

ClientScene.FindLocalObject(theNetworkId) // DEPRECATED

NetworkIdentity.spawned[theNetworkId] // USE THIS
12 Likes

Any idea why Why client scene does not contain a definition for FindLocalObject for me?

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

3 Likes

Much Thanks!

1 Like
NetworkServer.spawned[netId] //current as of 5-2022
1 Like

update: NetworkManager.SpawnManager.SpawnedObjects[objectID];

2 Likes
Utils.GetSpawnedInServerOrClient(netId) // 06/2024
2 Likes