Search Unity

Why is this returning Null?

Discussion in 'Scripting' started by NavpointSoftware, Apr 7, 2019.

  1. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    So I have some data i need to transfer across the Menu scene and the In game Scenes. Im using DontDestroyOnLoad(); to make sure that the holding object is not destroyed...the holding object contains the IAP script.

    When the user clicks the relevant menu button it calls WhenLevelSceneLoads(); ....I have put it in a separate function instead of Start because the Start() function will have already been loaded from the Menu scene.

    *user Presses Menu Button*

    Code (CSharp):
    1.     public void LoadNewACScene()
    2.  
    3.     {
    4.         ButtonSoundFX();
    5.         int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    6.         SceneManager.LoadScene(indexLevel);
    7.         scriptIAP.WhenLevelSceneLoads();
    8.  
    9.     }
    New Scene Loads and calls scriptIAP.WhenLevelSceneLoads()

    Code (CSharp):
    1.   public void WhenLevelSceneLoads()
    2.     {
    3.         scriptChecklistsUnlocked = FindObjectOfType<ChecklistUnlocks>();
    4.         Debug.Log(scriptChecklistsUnlocked);
    5.         StartCoroutine("OnLevelLoad");
    6.        
    7.     }
    I need access this script as part of a couple of things in the Co-routine.

    But scriptChecklistUnlocked is always returning null.

    I've also tried putting it within the called Co-routine but it still returns null - I was thinking that with all the loading of the rest of the scene it might be calling the function before the rest of the scene has finished setting itself up....but nope still returning null....what am i doing wrong?

    Any Help Much appreciated.

    Kind Regards,
    John
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    If the problem is
    scriptChecklistsUnlocked = FindObjectOfType<ChecklistUnlocks>();
    is getting null, then no object is being found that has a
    ChecklistUnlocks
    script on it.

    So check that an object with that script definitely exists and that it is enabled.

    If you want to do a sanity check and that script is called
    scriptIAP
    , then
    FindObjectOfType<ScriptIAP>()
    should find itself.

    Maybe try adding a debug log in the
    Start
    function of the
    ChecklistUnlocks
    to see that it exists by the time the find is being done.
     
  3. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    Thanks for the reply, backs up what I thought.

    It turns out that I was correct myself and after running more tests it was just the fact the object with the script hadn't loaded in time. Basically the delay on the Co-routine didn't update in the app for some reason so it was firing the functions without the delay which meant it was looking for a component that hadn't yet fully loaded and thus gave a null.