Search Unity

Bug Build only bug on first scene

Discussion in 'Scripting' started by jleven22, Mar 30, 2023.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    First time I'm getting a null reference on a very simple script, and it's only happening after I build out my game, not in editor play mode.

    The error:

    WindowsPlayer(LAPTOP-HDNF53UP) NullReferenceException
    at (wrapper managed-to-native) UnityEngine.GameObject.SetActive(UnityEngine.GameObject,bool)
    at IntroLevelLoader+<LevelEnd>d__10.MoveNext () [0x001dd] in C:\Users\movie\Desktop\Projects\YAD\Assets\Scripts\System\IntroLevelLoader.cs:53
    at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00026] in <1bcd79d45cd74590b726c010430e0a7b>:0

    (Filename: <1bcd79d45cd74590b726c010430e0a7b> Line: 0)


    As far as I understand, this is saying that the asset at cs:53 is not available or something. But... it is. See the script below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class IntroLevelLoader : MonoBehaviour
    8. {
    9.     public GameObject titleText, SFX, sparkOne, sparkTwo, sparkThree, sparkFour, sparkFive, sparkSix;
    10.  
    11.     public GameObject loadingIcon;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.         loadingIcon.SetActive(false);
    18.         titleText.SetActive(false);
    19.         SFX.SetActive(false);
    20.         sparkOne.SetActive(false);
    21.         sparkTwo.SetActive(false);
    22.         sparkThree.SetActive(false);
    23.         sparkFour.SetActive(false);
    24.         sparkFive.SetActive(false);
    25.         sparkSix.SetActive(false);
    26.  
    27.         StartCoroutine(LevelEnd());
    28.     }
    29.  
    30.     public IEnumerator LevelEnd()
    31.     {
    32.         yield return new WaitForSeconds(.75f);
    33.         titleText.SetActive(true);
    34.         sparkOne.SetActive(true);
    35.         SFX.SetActive(true);
    36.  
    37.         yield return new WaitForSeconds(.1f);
    38.         sparkTwo.SetActive(true);
    39.  
    40.         yield return new WaitForSeconds(.1f);
    41.         sparkThree.SetActive(true);
    42.  
    43.         yield return new WaitForSeconds(.1f);
    44.         sparkFour.SetActive(true);
    45.  
    46.         yield return new WaitForSeconds(.1f);
    47.         sparkFive.SetActive(true);
    48.  
    49.         yield return new WaitForSeconds(.1f);
    50.         sparkSix.SetActive(true);
    51.  
    52.         yield return new WaitForSeconds(.3f);
    53.         sparkOne.SetActive(false);
    54.         sparkTwo.SetActive(false);
    55.         sparkThree.SetActive(false);
    56.         sparkFour.SetActive(false);
    57.         sparkFive.SetActive(false);
    58.         sparkSix.SetActive(false);
    59.  
    60.  
    61.         yield return new WaitForSeconds(2);
    62.         titleText.SetActive(false);
    63.         loadingIcon.SetActive(true);
    64.  
    65.         yield return new WaitForSeconds(.5f);
    66.  
    67.  
    68.         if(PlayerPrefs.HasKey("tutorial"))
    69.         {
    70.             LoadFirstScene();
    71.         }
    72.         else
    73.         {
    74.             PlayerPrefs.SetInt("tutorial", 0);
    75.             PlayerPrefs.Save();
    76.  
    77.             PlayerPrefs.SetInt("shouldVibrate", 1);
    78.             PlayerPrefs.Save();
    79.  
    80.             LoadFirstScene();
    81.         }
    82.        
    83.  
    84.  
    85.  
    86.     }
    87.  
    88.     public void LoadFirstScene()
    89.     {
    90.  
    91.         if (PlayerPrefs.GetInt("tutorial") == 1)
    92.         {
    93.             SceneManager.LoadScene("Hub World");
    94.         }
    95.         else
    96.         {
    97.  
    98.             SceneManager.LoadScene("IntroPoem");
    99.         }
    100.     }
    101.  
    102. }
    103.  

    It's a super basic script, and has no reason not to play well. Any ideas on why this could be happening once I debug the build?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    It's just a nullref man. If your line number is correct (and often times the forum will subtly change your number!), then you can see
    sparkOne
    is null.

    If that's not it, then as always, it's always the same answer:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
    Bunny83 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Also, rather than splattering strings all over the place that can have typos like this:

    You might want to try a more-bundled-up approach like this:

    Here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

    Useful for a relatively small number of simple values.
     
  4. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Oh my god, so I guess I had the objects being destroyed before turning off. Outdated scripts.

    Wasted time for sure, sorry for wasting your time!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    It's all good... just like war, nullref never changes. :)
     
    Bunny83 likes this.
  6. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    All is fair in love and nullref
     
    Kurt-Dekker likes this.