Get reference to spawned object from server on client
I don't have a multiplayer game. I'm using my laptop as a server, and a Hololens as my client.
I've spawned a prefab on the server with client authority to the hololens, and it spawns correctly. I tried adding a MsgType.ObjectSpawned, but it is never called on the client side, so I have no reference to the spawned gameobject on my client. Essentially, that's all I'm trying to do, get a reference to the spawned gameobject on the client. I have three seperate gameobjects in my scene: a client, a server, and a clientServerManager. The clientservermanager makes whichever one active depending on the machine it's run on. This is because most of the code is different for both the server, and the client. The only thing they really need to know are the same classes/prefabs, and spawned objects.
A little bit about the actual thing I'm doing it for:
Hololens sends device position and spatial map mesh data in world space to server, which visualises this in the game view. The server can spawn objects wherever the hololens is looking towards, which works. I've added a directional indicator to the spawned object, so the user on the hololens can find it. Unfortunately, I added it the spawned object prefab, so it adds one for each new one. I want to have it on just the latest.
So I added my directional indicator script to the client, and make a currentObject
GameObject reference. I want to set the latest spawned object to this currentObject
.
Again, getting the reference will be useful for other things, but this is the specific use case I'm doing it for now.
Thanks!
EDIT:
Oh, also, I'm not using a NetworkManager. I'm just using NetworkServer on server, and a NetworkClient object on client.
Answer by Gentatsu · Oct 21, 2016 at 10:25 AM
I'm not sure if this is the best way to have handled it, but I attached a script to be spawned object prefab.
When it starts, I search for the direction indicator gameobject if it's a client. If it finds it, it sets the currentObject
to the gameobject prefab the script is attached to. This works, but I'm not sure if it's the correct way to go. The client doesn't still really know about it. The only way I can seem to interact with the client from the server spawned object is from the spawned object itself, instead of the other way.
hey did you learn the how to get spawned object refrence? i am searching it please share the solution
Your answer
Follow this Question
Related Questions
Spawning Dynamically Created Object (Network) 1 Answer
Simple server for Unity games. 0 Answers
Networking best practices for handling local player 0 Answers
Detect if mobile is on Tether connection 1 Answer
Are networked object pools possible? 0 Answers