- Home /
Multiple Spawn Points,Multiple Spawn Points in one scene
,So I have a game i'm making where there is a main room (scene) and it has little side rooms that come off of it (other scenes). The problem I have is about spawn points. I want to be able to enter another scene, and then when i exit that scene, I spawn near that door, instead of the first place you spawn in the game.
I've coded before but never really with C# and I've been able to do some v basic code, but this is a little beyond me, as i don't know the kinds of functions etc.
What I envision is a way to basically say "if (the scene before was scene 1) spawn point = this empty game object near the door, else if (scene before was scene 2 ) spawn point = different object , etc . i managed to do some code that enabled me to input a game object as the spawn point, but i could only do one, and it wasn't dependent on which scene i exited from.
Can anyone help me with like a basic way to code this? I know people don't usually get whole things of code in the answers, but if anyone could get me started that would be sick.
Answer by highpockets · May 12, 2020 at 09:13 PM
There are multiple ways that you can do this, but I think a simplistic approach could be to have a DoorManager class which persists between scenes, you can use DontDestroyOnLoad() to persist. And setup trigger colliders at each door which are have a tag name based on the location to spawn in the next scene and so you would have the position right when you enter the trigger.
void OnTriggerEnter(Collider other){
if(other.tag == “SouthDoorLobby”){
positionToSpawn = southDoorLobbySpawnPoint;
}
}
///After loading
transform.position = positionToSpawn;
@highpockets First off thank you for the quick reply and easy to read answer!
$$anonymous$$y next question is, so i have made a Door manager script, which is a Singleton correct? So i have made the script that supposedly holds information throughout the game, now do would i add this bit of code you've provided to the same script, or to another script that references my Door manager?
(i hope this makes sense and isn't a dumb question lmao)
Yes, that would be the idea. Just map out your doors with the trigger function:
void OnTriggerEnter(Collider other){
if(other.tag == “SouthDoorLobby”){
spawnPoint = southDoorLobbyPosition;
}
else if(other.tag == “SouthDoorKitchen”){
spawnPoint = southDoorKitchenPosition;
}
}
You can have all of those conditions in the trigger enter function on the door manager script.
Another way to do this would be to have a script on each of your doors. The Door class. If, for example, you have a lot of doors, this might be a better solution for you. Each door again would have a trigger collider attached to notify you when the player is there and then you could access that class from a script on the player. On the Door class:
//this little trick is so you can set spawn point in the inspector and then never change it’s value, but you can access it with the public getter.
[SerializeField]
private Vector3 _spawnPoint;
[HideInInspector]
public Vector3 spawnPoint{
get{ return _spawnPoint; }
}
Then on a script attached to the player:
void OnTriggerEnter(Collider other){
if(other.tag == “door”){
doorScript = other.gameObject.GetComponent<Door>();
spawnPoint = doorScript.spawnPoint;
}
}
That solution is more elegant I$$anonymous$$O
@highpockets hello i am back lmao. i put this game on hold and im common back to it now, and i am revisiting this page in order to execute this script, and I'm gonna ask if you can help me a little more with this code if thats alright lmao! I really appreciated your answer at the time, but since its been a $$anonymous$$ute and ive not been in the coding headspace, and i never finished the script at the time, i need to ask you a question.
if I have the Door$$anonymous$$anager class and it persists, i put this code to map out my doors, but 1) how to i plug in an actual "point" ie location to spawn where you've designated the example "south doorlobbyspawnpoint" and 2) how do i signify "//after loading", should i use a function that has to do with the scene loading or what?
Thank you I hope you see this and can help me more now that my brain has lost the coding lmao
Your answer
![](https://koobas.hobune.stream/wayback/20220612234108im_/https://answers.unity.com/themes/thub/images/avi.jpg)