Hi everyone !
I apologize in advance cause i’m a real big noob in c# and… code in general. And sorry for my english too
!
I’m just a french artist trying to make a simple detective game with Unity.
I found an old code on youtube about teleporting player scene to scene with door number.
Some of the functions were outdated but I managed to make it work. I added a “WorldNumber” value to indicate witch scene had the engine to load, and I manage to send a “DoorNumber” value to the scene manager I made. I know, that’s not a big deal, but for me that’s huge lol.
So, now, when I hit a door, my player goes to the right scene and the game manager stores the correct “DoorNumber” and “WorldNumber” values. But I have issues to manage one piece of code : the lines who transform my player’s position to the corresponding door. When player hits the door “1” in scene 0 for scene 0, he spawns in the right scene but not on the door marked as “1”.
here’s the code for the door :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
public int DoorNumber;
public int SceneNumber;
void OnTriggerStay()
{
if (Input.GetButtonDown("Fire1"))
{
SceneManager.instance.LoadScene(DoorNumber, SceneNumber);
}
}
}
and here is the scene manager :
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
public static SceneManager instance = null;
public GameObject Player;
public GameObject[] DoorArray;
public int currentDoorNumber;
public int NextWorldNumber;
void Awake()
{
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = this;
}
else if (instance != null)
{
Destroy(gameObject);
}
if (Player == null)
{
Player = GameObject.FindGameObjectWithTag ("Player");
}
if (DoorArray.Length == 0)
{
DoorArray = GameObject.FindGameObjectsWithTag ("Door");
}
}
void OnLevelWasLoaded()
{
Player = GameObject.FindGameObjectWithTag ("Player");
DoorArray = GameObject.FindGameObjectsWithTag ("Door");
for (int i = 0; i < DoorArray.Length; i++)
{
if (DoorArray[i].GetComponent<Door>().DoorNumber == currentDoorNumber)
{
Player.transform.position = DoorArray[i].transform.position;
}
}
}
public void LoadScene (int WorldNumber, int DoorNumber)
{
NextWorldNumber = WorldNumber;
currentDoorNumber = DoorNumber;
UnityEngine.SceneManagement.SceneManager.LoadScene(NextWorldNumber);
}
}
I know my code’s modification is not the most efficient, but I’m already proud to succeed to modify an old piece of code and improve it in my way. i’ll be glad to have some help to finish it, understand it and finally see my player spawning on the right door ![]()
thanks to all, best regards
Léo