- Home /
GameObject.Find only working on server?
I'm instantiating GameObjects with the tag "Spawn" to create spawn points in my multiplayer game.
Network.Instantiate(spawnPoint, new Vector3(0, 6, 0), Quaternion.identity, 0).name = "SpawnPoint0";
Network.Instantiate(spawnPoint, new Vector3(0, 6, 15), Quaternion.identity, 0).name = "SpawnPoint1";
Network.Instantiate(spawnPoint, new Vector3(15, 6, 15), Quaternion.identity, 0).name = "SpawnPoint2";
Network.Instantiate(spawnPoint, new Vector3(15, 6, 0), Quaternion.identity, 0).name = "SpawnPoint3";
When a player connects, I use GameObject.FindGameObjectsWithTag() to find all of the spawn points and select a random one.
var spawners:GameObject[] = GameObject.FindGameObjectsWithTag("Spawn");
Debug.Log(spawners.Length);
var spawnNum:int = Random.Range(0, 3);
Debug.Log(spawnNum);
spawnPos = spawners[spawnNum].transform.position;
It works on the server, but on the client GameObject.FindGameObjectsWithTag
returns null. What am I doing wrong?
Answer by Yokimato · Aug 15, 2013 at 05:53 PM
This won't solve the issue, however it's a possible work around.
I'm doing something similar however, each "Map" has the spawn points already predetermined already as I didn't see a need for creating them on the fly since I (and possibly you) only want the transform.Position of them to tell the player to instantiate there.
Each map has a bunch of gameobjects scattered with a spawnPoint tag and that I fetch like you're doing and pull a random one and use it's position for my network instantiate of my player.
To make a long story short, is there a need to make the spawn points network instiantiated if the vector positions are hardcoded anyway?
(again not sure why yours isn't working, but the way above does work for me at least, hope this helps.)
I don't have separate scenes for each map - I instantiate different spawn points depending on what map the host selects. I could add the spawn points beforehand though, and add a variable to them saying what map they are for. Thanks for your help, I'll see if this works!
That might be a great solution -- if you're reusing the same scene, have a data structure that has the "map" -> List of spawn points with their positions. Then when "switching", your code can just run to change the current spawnpoints to match the list for the map.
That worked great - I made a static class with lists for each of my map's spawn points. Thanks!
Your answer
Follow this Question
Related Questions
Adjusting Rotation of Instantiated Objects 0 Answers
Searching for gameobjects with a tag and creating a button for each one? 1 Answer
How can I destroy an specific clone already instantiated, when there's more than one? 1 Answer
GameObject.FindObjectWithTag("GameManager") doesn't work [C#] 1 Answer
Instantiating gameObejcts that can't be referred to after creation 1 Answer