- Home /
Loading Spawn points based on decision
Hi Everyone
I have this code I'm trying to alter to fit my game style but I'm having an issue.
What I want is once the player picks his or her team that will decide which Spawn area they will start in.So If they pick Red team they will Spawn where the red team should and the same idea with the blue team. Here is the code that handles the choice. I got this code from http://www.M2H.nl)
private var chooseTeamInt = 0; private var chooseTeamNames : String[] = ["Red", "Blue"]; private var ffInt = 0; private var ffNames : String[] = ["Off", "On"]; private var chosenPlayerInt = 0;
if (serverGameTypeSelected != "DM") { GUI.Label (Rect (110, 120, 150, 20), "Choose Team: "); chooseTeamInt = GUI.Toolbar ( Rect (250, 120, 100, 20), chooseTeamInt, chooseTeamNames);
switch (chooseTeamInt)
{
case 0:
PlayerPrefs.SetString("playerTeam", "RedTeam");
break;
case 1:
PlayerPrefs.SetString("playerTeam", "BlueTeam");
break;
}
This is the code that handles the loading of the player when the game type is chosen.
void OnNetworkLoadedLevel() {
GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoints");
Debug.Log("Spawns: " + spawnPoints.Length);
Transform spawnpoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)].transform;
Transform newTrans = Network.Instantiate(playerPref, spawnpoint.position, spawnpoint.rotation, 0) as Transform;
}
Answer by DaveA · Mar 26, 2012 at 08:33 PM
Why not have tags like "RedSpawnPoints" and "BlueSpawnPoints" and assign them accordingly?
Its s network game so it would depend first on the choice I do have those tags set up my problem is that I'm still new to network games so the company I work for want it done this certain way so I can't do it another way. They want the spawn to be deter$$anonymous$$ed but what $$anonymous$$m the player picks and spawn in that area according the spawn areas set in the Editor
Yeah so you can do that. When the player makes his choice, you send that to other players, and when that choice is received, it picks the matching set of spawnpoints by tag (using tags like "RedSpawnPoints" ins$$anonymous$$d of simply "SpawnPoints")
Here is the updated code I did now I tried doing that and now I get this error Tag:RedSpawn undefined. I'll put it in the answers field
I even tried doing this
void OnNetworkLoadedLevel() {
if (boolean == false)
{
GameObject[] RedSpawnPoint = GameObject.FindGameObjectsWithTag("RedSpawnPoint");
Transform spawnpoint = RedSpawnPoint[UnityEngine.Random.Range(0, RedSpawnPoint.Length)].transform;
Transform newTrans = Network.Instantiate(playerPref, spawnpoint.position, spawnpoint.rotation, 0) as Transform;
}
else if(boolean == true)
{
GameObject[] BlueSpawnPoint = GameObject.FindGameObjectsWithTag("BlueSpawnPoint");
Transform spawnpoint = BlueSpawnPoint[UnityEngine.Random.Range(1, BlueSpawnPoint.Length)].transform;
Transform newTrans = Network.Instantiate(playerPref, spawnpoint.position, spawnpoint.rotation, 0) as Transform;
}