Teleport System

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 :wink: !
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 :wink:

thanks to all, best regards
Léo

Have you checked to make sure all the expected scenes are present in your Build Settings?

Hi ! Thanks for you reply.
Yes, I’ve checked this and I don’t have problems to switch between scenes, but to transform character position at the right point !

I think the problem comes from this code part :

 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;
            }
        }
    }

Ok, so I tried some tricks to understand where it’s not working.
I added this line to see if my script finds the Player and the correct Door when the new starts and he is :

Debug.Log("Hello" + DoorArray[i] + Player);

The script just don’t want to transform my player position based on the DoorArray position !

Are you sure the void OnLevelWasLoaded() function is even being called?

That function has been deprecated for a loooong time now (years and years) and will probably soon just stop being called silently.

1 Like