Some basic code I have written to change scenes works on my Laptop but not on my Desktop. I doubt it’s a problem with the code but I will still explain it all here. When we move to a new scene not only do we have to identify which scene but we need to enter which door to spawn the player at in this scene. To do this I assign an int variable to an object that has “DontDestroyOnLoad”. They reference that int as an index position of a list that contains all the spawn points in that room
Here is the door code:
public class DoorManager : MonoBehaviour
{
[SerializeField] private GameObject levelManager;
[SerializeField] private int nextScene;
[SerializeField] private int sceneDoor;
[SerializeField] private float openRange;
[SerializeField] private GameObject doorCentre;
[SerializeField] private GameObject playerBody;
private GameObject levelData;
private Animator anim;
void Start()
{
levelData = GameObject.Find("LevelData");
anim = GetComponent<Animator>();
}
void Update()
{
anim.SetBool("isOpen", Vector3.Distance(playerBody.transform.position, doorCentre.transform.position) < openRange);
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
if (levelData != null)
{
levelData.GetComponent<LevelData>().ChangeSpawnDoor(sceneDoor);
}
levelManager.GetComponent<LevelManagment>().SceneTransition(nextScene);
}
}
}
ChangeSpawnDoor() on the “LevelData” component just updates a variable in that script to the parameter we feed in like so. It also has the DontDestroyOnLoad feature:
public class LevelData : MonoBehaviour
{
public int doorIndex;
[SerializeField] private GameObject self;
private GameObject[] duplicates;
// Network state
void Start()
{
DontDestroyOnLoad(self);
}
void Update()
{
duplicates = GameObject.FindGameObjectsWithTag("LevelData");
if(duplicates.Length > 1)
{
Destroy(duplicates[1]);
}
}
public void ChangeSpawnDoor(int doorValue)
{
doorIndex = doorValue;
}
}
Finally, this is the Level Manager script. The function previously called, SceneTransition() just changes the scene to what we feed in, ignore PlayerBeatLevel() and ConnectedLevelBeat(), that is for a different feature.
public class LevelManagment : MonoBehaviour
{
private bool isComplete;
private bool isLevelOn;
private GameObject levelData;
[SerializeField] private GameObject player;
[SerializeField] private GameObject[] connectedLevels;
[SerializeField] private Transform[] spawnPoints;
private void Start()
{
levelData = GameObject.Find("LevelData");
if (levelData != null)
{
player.transform.position = spawnPoints[levelData.GetComponent<LevelData>().doorIndex].position;
}
}
public void SceneTransition(int _nextScene)
{
SceneManager.LoadScene(_nextScene, LoadSceneMode.Single);
}
public void PlayerBeatLevel()
{
isComplete = true;
isLevelOn = !isLevelOn;
}
public void ConnectedLevelBeat()
{
isLevelOn = !isLevelOn;
}
}
The line in the start function should set the player’s position to the predefined spawn point whose index matches the predefined door variable.
All of this code works on my laptop and it works consistently however when I upload to GitHub and download it on the desktop, the line of code in the start function of level manager does not run. The player’s scene changes but they are not put in front of any door. They are left at the point where the player game object is in the editor.
Any help is appreciated, I have been looking online and trying to bug fix myself for about 2 weeks. Also, let me know if there is a better way to manage scenes with multiple exit and entry points