Search Unity

Script does not load when loading a scene an second time?

Discussion in 'Scripting' started by TheoEsberg, Nov 14, 2019.

  1. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    So I am currently working on a project of mine and I have ran in to a problem that I just cant figure out. So the problem is this, I have created an start menu that runs a script when starting the game. Everything loads and the animations is loaded correctly BUT when I go to the main "game" scene and then go back to my "menu" scene the script just does not load. It is greyed out in the GameObject that holds the script, why does it not want to load correctly when loading from another scene but loads correctly when loading the game in the begining?


    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.Animations;
    7.  
    8. public class StartTextAnimations : MonoBehaviour
    9. {
    10.     public float welcomeDelay = 0.2f;
    11.     public float teamTreesDelay = 0.1f;
    12.     public float animDelay = 0.5f;
    13.     public string fullWelcome;
    14.     public string fullTeamTrees;
    15.     public string currentWelcome = "";
    16.     public string currentTeamTrees = "";
    17.     public Text teamTrees;
    18.     public Text welcome;
    19.  
    20.     public Text startText;
    21.     public Text quitText;
    22.     public GameObject startButton;
    23.     public GameObject quitButton;
    24.     public Animator startButtonAnim;
    25.     public Animator quitButtonAnim;
    26.  
    27.     public bool startTheScene;
    28.  
    29.     void Start()
    30.     {
    31.         startTheScene = true;
    32.         startButton.SetActive(false);
    33.         quitButton.SetActive(false);
    34.         startButtonAnim = startButton.GetComponent<Animator>();
    35.         quitButtonAnim = quitButton.GetComponent<Animator>();
    36.     }
    37.  
    38.     void OnLevelWasLoaded(int level)
    39.     {
    40.         StartCoroutine(WelcomeText());
    41.     }
    42.  
    43.     void Update()
    44.     {
    45.         if (startTheScene == true)
    46.         {
    47.             StartCoroutine(WelcomeText());
    48.             startTheScene = false;
    49.         }
    50.  
    51.         if (currentWelcome.Length == 0)
    52.         {
    53.             StartCoroutine(WelcomeText());
    54.         }
    55.     }
    56.  
    57.     IEnumerator WelcomeText()
    58.     {
    59.         for(int i = 0; i < fullWelcome.Length; i++)
    60.         {
    61.             currentWelcome = fullWelcome.Substring(0, i);
    62.             welcome.GetComponent<Text>().text = currentWelcome;
    63.             yield return new WaitForSeconds(welcomeDelay);
    64.             if (currentWelcome.Length == fullWelcome.Length - 1)
    65.             {
    66.                 StartCoroutine(TeamTreesText());
    67.             }
    68.         }
    69.     }
    70.  
    71.     IEnumerator TeamTreesText()
    72.     {
    73.         for (int i = 0; i < fullTeamTrees.Length; i++)
    74.         {
    75.             currentTeamTrees = fullTeamTrees.Substring(0, i);
    76.             teamTrees.GetComponent<Text>().text = currentTeamTrees;
    77.             yield return new WaitForSeconds(teamTreesDelay);
    78.             if (i > 10)
    79.             {
    80.                 startButton.SetActive(true);
    81.                 quitButton.SetActive(true);
    82.             }
    83.         }
    84.     }
    85.  
    86.     public void BigStartText()
    87.     {
    88.         startText.fontSize = startText.fontSize + 10;
    89.     }
    90.  
    91.     public void SmallStartText()
    92.     {
    93.         startText.fontSize = startText.fontSize - 10;
    94.     }
    95.  
    96.     public void BigQuitText()
    97.     {
    98.         quitText.fontSize = quitText.fontSize + 10;
    99.     }
    100.  
    101.     public void SmallQuitText()
    102.     {
    103.         quitText.fontSize = quitText.fontSize - 10;
    104.     }
    105.  
    106.     public void StartGame()
    107.     {
    108.         SceneManager.LoadScene("game");
    109.     }
    110.  
    111.     public void QuitGame()
    112.     {
    113.         Application.Quit();
    114.     }
    115. }
    116.  



    All help is greatly appriciated, and Im sorry if the code is a bit funky and not that greatly created, im a bit new to this :/
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    What happens if you comment out the OnLevelLoaded() ? I don't think that fires when you are ON a level and press PLAY, but I could be wrong. That would at least be one code flow difference.
     
  3. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
  4. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    I tried it but nothing changes, the script is still not running when loading the scene :/
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Time to either attach the debugger or start sprinking strategic Debug.Log() statements in to find out if any of the code is even running or not, or if it's running and failing for other reasons. I take it there are no error messages in console?
     
  6. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    Nope, not a single error message, I ran some simple Debug.Log() and found out that the script is actually running, the Start() is running fine and so is the Update() but the problem is with the IEnumerator and this part of the code:
    Code (CSharp):
    1.  IEnumerator WelcomeText()
    2.     {
    3.         for(int i = 0; i < fullWelcome.Length; i++)
    4.         {
    5.             currentWelcome = fullWelcome.Substring(0, i);
    6.             welcome.GetComponent<Text>().text = currentWelcome;
    7.             yield return new WaitForSeconds(welcomeDelay);
    8.             if (currentWelcome.Length == fullWelcome.Length - 1)
    9.             {
    10.                 StartCoroutine(TeamTreesText());
    11.             }
    12.         }
    13.     }
    I did an Debug.Log(i) and found out that for some wierd reason after the second load "i" is always "0". The Debug.Log() just printed that i was 0 all the time and that is why it is not loading the text. Got any ideas on a fix?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    What happens if you just wait for 1 second and then Start the TeamTreesText() coroutine?

    Think of ways to eliminate portions of your code from consideration... chop it down until the only part you have left is the malfunctioning part. Also, use source code then it is trivial to restore any damage to your code when you finally find and fix the problem.
     
  8. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    Thanks for the tip, I started the TeamTreesText() coroutine but the same thing happend there, "i" was ecual to "0" all the time :/
     
  9. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    Actually not only did "i=0", it only ran once, hmm...
    Code (CSharp):
    1. IEnumerator TeamTreesText()
    2.     {
    3.         Debug.Log("TeamTrees Corutine is launched!");
    4.         for (int i = 0; i < fullTeamTrees.Length; i++)
    5.         {
    6.             Debug.Log(i);
    7.             currentTeamTrees = fullTeamTrees.Substring(0, i);
    8.             teamTrees.GetComponent<Text>().text = currentTeamTrees;
    9.             yield return new WaitForSeconds(teamTreesDelay);
    10.             if (i > 10)
    11.             {
    12.                 startButton.SetActive(true);
    13.                 quitButton.SetActive(true);
    14.             }
    15.         }
    16.     }
     
  10. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Are you entering the if every time or are you skipping it?
    Add a Debug.Log and see if it gets called when you enter the scene a second time. If it doesn't, then you startTheScene variable is causing issues.

    Code (CSharp):
    1.         if (startTheScene == true)
    2.         {
    3.             Debug.Log("Entered If");
    4.             StartCoroutine(WelcomeText());
    5.             startTheScene = false;
    6.         }
     
  11. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    I did try that but that was not the problem, so I removed that completely since this did the same as the line below and it was unnecessary to call it twice :/
    Code (CSharp):
    1.  
    2.     void Update()
    3.     {
    4.         if (currentWelcome.Length == 0)
    5.         {
    6.             StartCoroutine(WelcomeText());
    7.         }
    8.     }
    9.  
     
  12. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Ah, so you're telling me that even now, your coroutine isn't being called? Try adding a debug.log in the else statement that prints out the value of currentVelcome. Maybe you're editing it somewhere outside the script?
     
  13. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    Well, The problem is not that the coroutine isn't being called, the problem is that the first run "i" gets one bigger each time aka "i++" but the second run "i" is not getting bigger the Debug.Log(i) is just printing "0" :/
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    break things apart more:

    1. take the length of the string on start to a separate variable
    2. print it - is it what you expect?
    3. do your loop for that many iterations
    4. instead of testing / detecting equality in the loop, just wait for the loop to finish, THEN do the load... it's virtually the same end condition in any case, right?
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Also, did you change the value of Time.timeScale anywhere, like set it to zero?
     
  16. TheoEsberg

    TheoEsberg

    Joined:
    Nov 14, 2019
    Posts:
    9
    Omg I feel like an total idiot right now, Thank you soo much Kurt-Dekker, I had changed the Time.timeScale = 0 when i opened the "Esc" menu in the main game. Wow, anywas thanks a lot!
     
    Kurt-Dekker likes this.
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You're welcome. I have only done that seven separate times myself. You're ahead of me with only one!
     
  18. Codeman1010

    Codeman1010

    Joined:
    Jan 26, 2014
    Posts:
    6
    So here I am in Sept of 2020 and I did the same thing, paused my game with timescale and returned to the main menu. Thanks for the solve!
     
  19. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,060
    thanks
     
    Kurt-Dekker likes this.
  20. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,060
    yeah that is true, It was also my case
     
  21. radamedeiros

    radamedeiros

    Joined:
    Jan 8, 2016
    Posts:
    2
    Life saver :D Thank you
     
  22. Sebithial

    Sebithial

    Joined:
    Jun 22, 2013
    Posts:
    1
    And here we are in 2024 doing similar things :D