- Home /
one scene multiple spawn points
Hey! Please refrain from roasting me as I am a new developer and inexperienced coder but in my game I have a main room which has doors which load different scenes. I'd like to return to the scene with a spawn point for the player which is as if he had just opened the door from the room. I know I can do this by making multiple copies of the scene but that is tedious and not sustainable.
I'd like to be able to call to assign the gameobject and correct transforms for the player through the level loader/door script in the last scene heres what I have
using UnityEngine;
using System.Collections;
public class PlayDoor : MonoBehaviour {
public int button1function;
public float width;
public Transform player; //transform for the player
public float height;
public GameObject playercontroller; //gameobject of player
public string ScreenText;
private bool enter;
void OnTriggerEnter ( Collider other ){
enter = true;
}
void OnGUI (){
if (enter) {
GUI.Label (new Rect (Screen.width / 2 - width, Screen.height - height, 150, 30), ScreenText);
if (Input.GetKeyDown ("f")) {
audio.Play ();
AutoFade.LoadLevel (button1function, (1 / 2), 1, Color.black);
}
}
}
void OnLevelWasLoaded(int level) //not sure if this is even possible or the correct usage of onlevelwasloaded, but to my understanding it calls something to be done right as the next scene was loaded? {
if (level == 0)
Instantiate (playercontroller,transform.position, transform.rotation );
}
void OnTriggerExit ( Collider other ){
enter = false;
}
}
If anyone knows how to fix this code/have multiple spawns in a simpler way and can point me in the right direction please let me know. My apologies if this answer is self evident and I am just lost.
Answer by Andres-Fernandez · Nov 25, 2014 at 08:31 AM
You almost have said it yourself: "I'd like to be able to call to assign the gameobject and correct transforms for the player through the level loader/door"
What you need is one persistent script that holds the position of the player when the scene loads. You need to create an object, use DontDestroyOnLoad to make it persistent (also use a singleton pattern to avoid duplicating the object, just google "unity singleton") and make it have a reference to the spawn point. When using a door, store the reference to that spawn point in your persistent object. Then, whenever you reload the scene, the persistent script can instantiate the player in the correct position.