Nina, thank you for your persistence. I really appreciate it.
Ok here goes…
I’m trying to do a metroid-like level system where I can go in a door (to a new scene) and when I come back through that door I start at the point I left from.
I have a GameManager object that doesn’t destroy on load to hold information, and I have all of my spawn points set in it so that they don’t get deleted. The spawns are children of the GameManager that I point to in the inspector. This is it’s script.
using System.Collections;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using UnityEngine;
public class GameManagerScript : MonoBehaviour
{
GameObject player;
Transform currentSpawnTransform;
Scene currentScene;
string newSceneName = "null"; // declaring variables so that they are never null
//variables for level spawns
//simpleOver
[SerializeField]GameObject simpleOverspawn01;
[SerializeField]GameObject simpleOverspawn02;
public static int simpleOverSpawnLocationInt;
//simpleUnder
[SerializeField]GameObject simpleUnderspawn01;
[SerializeField]GameObject simpleUnderspawn02;
public static int simpleUnderSpawnLocationInt;
// called first
void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;//declares the OnSceneLoaded Method
}
// called second
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
newSceneName = SceneManager.GetActiveScene().name;
GameObject player = GameObject.FindGameObjectWithTag("Player");
// controls for spawning player into correct position based off what level player came from previously
if (newSceneName == "simpleOver")
{
Debug.Log("we are in simplover and should be placing the player in the right spot");
if (simpleOverSpawnLocationInt == 1)
{
currentSpawnTransform = simpleOverspawn02.gameObject.GetComponent<Transform>();
player.transform.position = currentSpawnTransform.position;
}
}
else if (newSceneName == "SimpleUnder")
{
Debug.Log("we are in SimpleUnder and should be placing the player in the right spot");
if (simpleUnderSpawnLocationInt == 1)
{
currentSpawnTransform = simpleUnderspawn02.gameObject.GetComponent<Transform>();
player.transform.position = currentSpawnTransform.position;
}
else if (simpleUnderSpawnLocationInt == 0)
{
currentSpawnTransform = simpleUnderspawn01.gameObject.GetComponent<Transform>();
player.transform.position = currentSpawnTransform.position;
}
}
}
//Destroys the gamemanager object in the scene that is loading if there are two, since we don't want multiples.
void Awake()
{
currentScene = SceneManager.GetActiveScene();
GameObject player = GameObject.Find("Player");
GameObject[] objs = GameObject.FindGameObjectsWithTag("GameManager");
if (objs.Length > 1)
{
Destroy(this.gameObject);
}
DontDestroyOnLoad(this.gameObject);
}
// Start is called before the first frame update
void Start()
{
//currentScene = SceneManager.GetActiveScene();
}
// Update is called once per frame
void Update()
{
Debug.Log("simpleunderspawnlocationint = " + simpleOverSpawnLocationInt);
if (currentScene.name == "UpUpAndAway")
{
GameObject currentMusicPlayer = GameObject.Find("simpeOverMusicPlayer");//get music player from first level
if (currentMusicPlayer != null)
{
Destroy(currentMusicPlayer.gameObject); //destroy 1st musicplayer
}
}
else if (currentScene.name == "ThroughTheMountain")
{
GameObject currentMusicPlayer = GameObject.Find("UpUpMusicPlayer");//get music player from UpUpandAwayLevel
if (currentMusicPlayer != null)
{
Destroy(currentMusicPlayer.gameObject); //destroy 2cnd musicplayer
}
}
else if (currentScene.name == "End")
{
GameObject currentMusicPlayer = GameObject.Find("MountainMusicPlayer");//get music player from UpUpandAwayLevel
if (currentMusicPlayer != null)
{
Destroy(currentMusicPlayer.gameObject); //destroy 2cnd musicplayer
}
// GameObject mySelf = GameObject.Find("GameManager");//get music player from UpUpandAwayLevel
// Destroy(mySelf.gameObject);
}
else if (currentScene.name == "OpeningMenu")
{
GameObject simpeOverMusicPlayer = GameObject.Find("simpeOverMusicPlayer");//get music player from UpUpandAwayLevel
GameObject UpUpMusicPlayer = GameObject.Find("UpUpMusicPlayer");//get music player from UpUpandAwayLevel
GameObject MountainMusicPlayer = GameObject.Find("MountainMusicPlayer");//get music player from UpUpandAwayLevel
if (simpeOverMusicPlayer != null)
{
Destroy(simpeOverMusicPlayer.gameObject); //destroy 2cnd musicplayer
}
else if (UpUpMusicPlayer != null)
{
Destroy(UpUpMusicPlayer.gameObject); //destroy 2cnd musicplayer
}
else if (MountainMusicPlayer != null)
{
Destroy(MountainMusicPlayer.gameObject); //destroy 2cnd musicplayer
}
}
}
}
each level has it’s own spawn controller script like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleUnderSpawn : MonoBehaviour
{
GameObject player;
[SerializeField] GameObject gameManager;
[SerializeField] int spawnLocationInt;
void Start()
{
gameManager.GetComponent<GameManagerScript>();
}
private void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player")
{
GameManagerScript.simpleUnderSpawnLocationInt = spawnLocationInt;
}
}
}
and this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleOverSpawn : MonoBehaviour
{
GameObject player;
[SerializeField] GameObject gameManager;
[SerializeField] int spawnLocationInt;
void Start()
{
gameManager.GetComponent<GameManagerScript>();
}
private void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player")
{
GameManagerScript.simpleOverSpawnLocationInt = spawnLocationInt;
}
}
}
I keep thinking there must be an easier way to do this, because this is tedious and really starting to frustrate me. I can get this to work when I leave the first level and come back, but it will not work on the second level, and I haven’t figured out why yet. edit I figured out it was because I had the wrong script on the third level. Nowe everything works the way I wanted it to. It’s just tedious
The whole reason I wanted to declare variables in the inspector was to avoid making a separate spawn script for every level.