Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Issue with loading a scene.

Discussion in 'Scripting' started by MachineHead, Sep 24, 2021.

  1. MachineHead

    MachineHead

    Joined:
    Jan 6, 2016
    Posts:
    30
    I have a start page scene that leads to the game scene when you press start. It does this fine, but only once.When the game is over due to the player having no lives left, the game manager reloads the start screen scene.

    However, when I press start again, it won't load the game screen again.

    I don't know what is causing it. My guess that it didn't properly reset the main game scene. So maybe the game over method I implemented when lives equal 0 is still running, but I don't know. Seeing that I created a load scene button to function During the gameplay and it resets everything including lives. It's the same code the Start scene uses to load initially as well.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,364
    Gonna need to know more details, including maybe some code in CODE tags. I suspect you are setting something at the end of your start screen which isn't being reset, so it skips it on the repeat.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Did you set Time.timeScale to zero?

    Did you destroy the EventSystem?

    Whatever it is, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  4. MachineHead

    MachineHead

    Joined:
    Jan 6, 2016
    Posts:
    30
    Start screen:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class MainMenu : MonoBehaviour {
    6.  
    7.  
    8.     // Use this for initialization
    9.     private void Awake()
    10.     {
    11.         Application.targetFrameRate = 30;
    12.     }
    13.     void Start () {
    14.    
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         //This is code for both controller and keyboard
    21.         if (Input.GetButton("Start") || Input.GetKeyDown("n"))
    22.         {
    23.             SceneManager.LoadScene("Main");
    24.             Debug.Log("Scene loaded");
    25.         }
    26.     }
    27. }
    28.  
    The "Main" scene thats loaded,basically where the game takes place, the Game Manager script has the same code in the Update method.
    Debug.Log isn't showing up at all for some reason. It was a minute ago when I was testing collisions.
    Edit.
    Debug.log is working again, somehow turned off the notices in the Console window.
     
    Last edited: Sep 24, 2021