- Home /
Player does not go to spawn point when I added a new scene
So, I have set up a start point script that finds all the game objects with the script playercontroller and cameracontroller and upon starting the scene it will teleport the player on the gameobject the script is attached to. The script is: private playercontroller thePlayer; private cameracontroller theCamera;
void Start()
{
thePlayer = FindObjectOfType<playercontroller>();
thePlayer.transform.position = transform.position;
Debug.Log(thePlayer);
theCamera = FindObjectOfType<cameracontroller>();
theCamera.transform.position = new Vector3(transform.position.x, transform.position.y, theCamera.transform.position.z);
Debug.Log(theCamera);
}
In both scenes inside the house scene and outside the house scene I have assigned 2 gameobjects with the same script both placed at where I want the player to teleport to when the scene loads. The scene load script is:
public string nextScene;
public string mainNext;
private bool isInZone;
void Update()
{
if (isInZone && Input.GetKey(KeyCode.E))
{
SceneManager.LoadScene(nextScene);
}
}
private void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.name == "Max")
{
isInZone = true;
} else
{
isInZone = false;
}
}
public void startScene()
{
SceneManager.LoadScene(mainNext);
}
These works fine, the player teleports to the start point i have assigned. But the problem is, as soon as I added a new Main menu scene which I added a simple button which upon clicked on, it will switch the scene to outside of my house scene. That part is fine too, and when I enter the house it goes to where I want it to, but then the second time I come back, the character doesn't spawn at the spawn point anymore, instead it spawned at the location where he was last scene. This only occurs when I started playing the game at the Main menu. If I start at outside of my house, it works fine going in and out multiple times. I am extremely confused as to why this is happening. (I'm new to programming by the way, so, sorry if this is a nooby question)
Little update on this one. I tested out with debug.log and started tracking the character's transform.position. When I switched back to start up scene it says the object is destroyed. I will look into fixing that but I am still wondering why does starting from main menu scene and starting from start up scene would lead to different outcomes
Answer by GrayLightGames · Oct 15, 2019 at 09:38 PM
Hi @DkSker, are you saying you have 2 objects in the same scene that have the start point script? If so, it might be unpredictable which one is being executed first and that may explain that behavior. Or is it just one per scene? If you truly only have one start point script in each scene, it looks like Start is not executing in the error situation. You can find out if Start is executing with Debug.Log statements. Start should execute when the scene loads, unless the object has DontDestroyOnLoad.
Are you using DontDestroyOnLoad for anything? Do you persist the player between scenes, or is there a separate player object in each scene?
It's hard to say what's happening without more information, but one thing about programming is you can always trust there is a solution, so hang in there!
Thank you for replying! And yes, I have 1 gameobject that act as a start point in each of the 2 scenes I set up. Both of the game object have that start point script attached to it. (The first code I posted above). And yes, both the camera and main character controller script have DontDestroyOnLoad code in it. And added a piece of code to stop the duplication from happening by checking if there is a player in that scene already.
Everything works according to my plan. Like the character teleport to the start point i assigned no matter how many times I enter or exit the house. All of these works if I start playing the game at my start up scene which is the outside of my house. But as soon as I added a new main menu scene and attached a script to the button that loads my start up scene (startup) when the button is clicked using Scene$$anonymous$$anager.LoadScene, the character will spawn at the right place at first in the start up scene then to the correct coordinate to the inside of the house scene then when I exit after that, it goes to the coordinate the player has in the previous scene which is the inside_house scene.
What is bothering me is that it works if I don't start the game at the main menu scene. I couldn't come up with any correlation between the error and the new scene.
The first picture is the character inside the house at the exit. note his coordinate. and after exiting the scene to outside scene which is the startup scene, he got teleport to the same coordinate in that separate scene despite having the start point game object set up in both scenes. (I started this on the main menu scene.)
It's odd, it sounds like either Start() isn't executing when you exit the house and reload scene 1 or it's not finding your player object properly. Just for now, can you set the player object in your start point script to public so you can see it in the inspector? Check to see that when the issue occurs, it has a linkage to your player object. You can also just Debug.Log("START ON SCENE 1 START POINT RUNNING") to see if Start() does actually execute.
Here's what I'm thinking as well... that code you mentioned that prevents duplication is one implementation of the Singleton pattern, in case you haven't heard it called that before. Do you have a player object in your main menu scene? Either way, it might be that the start point script is locating the scene's default player object before it is destroyed. So it moves the scene 1 player object to the start location before the player object's Start() runs, but then the scene 1 player object's Start() runs and destroys itself. It could be that starting from main menu changes the Start() execution order. If I'm right, thePlayer in your start point should be "null" or "missing" in the inspector when you notice the issue (because it was linked but then it was destroyed). You could verify by adding a public field called sceneID to your player object. When you're in scene 2 in play mode, manually change the name on the DontDestroyOnLoad player with the inspector to something unique. In your start point script, log the name of the object that is found. If it is the default name for scene 1, then what I'm describing is happening.
There's a lot of Singleton pattern implementations out there, but here's the one I use with an example class called YourClassName:
private static YourClassName instance = null;
public static YourClassName Instance
{
get { return instance; }
}
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
return;
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
Then to retrieve the current Singleton object, you call this from any script:
yourClassSingleton = YourClassName.Instance;
This way, you don't need to find by type, you just retrieve the instance from the class. This should guarantee that you will always find the right instance. Your implementation is missing a direct way to access the current instance, so adding one should remove the chance of your script not finding the right instance. If that's what is indeed occurring.
Strange bug, keep trying! Debug.Log is your friend :)
Hey, I found a solution! All I had to do was ins$$anonymous$$d of leaving the dontdestroyonload character camera in the start up scene, I dragged their prefab to the main menu scene and then change its position.z to -100 so it wont be visible on screen and when I do that, everything is fine. Thanks for your help!
Answer by DkSker · Oct 16, 2019 at 12:24 AM
the Dontdestroyonload code that is on the character controller and camera controller is:
private static bool playerExists = false;
private void Start()
{
if(!playerExists)
{
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
} else
{
Destroy(gameObject);
}
}