Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DkSker · Oct 15, 2019 at 07:28 PM · scene-switchingspawnpoints

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DkSker · Oct 16, 2019 at 12:16 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DkSker · Oct 15, 2019 at 11:18 PM 0
Share

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.) alt text

alt text

wechat-image-20191015161224.png (189.0 kB)
image2.png (178.5 kB)
avatar image GrayLightGames DkSker · Oct 16, 2019 at 04:06 AM 0
Share

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 :)

avatar image DkSker GrayLightGames · Oct 16, 2019 at 05:04 AM 0
Share

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!

Show more comments
avatar image
0

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


 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reload a scene without closing the inspector and hierarchy 0 Answers

Ad no longer ready/available after loading new scene. (Unity Ads) 1 Answer

How To Save Health Between Scenes 1 Answer

Different spawn spots in Multiplayer 1 Answer

Instantiate script crashes unity 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges