Search Unity

How to manage a list from another script

Discussion in 'Scripting' started by SoontirF, Sep 25, 2018.

  1. SoontirF

    SoontirF

    Joined:
    Apr 24, 2018
    Posts:
    7
    Hi,

    I am having problems managing a list of an asset (Easy Main Menu asset) and there is no documentation in the asset about how to do this.

    This asset has a script called LevelSelectManager, where we can find a list like this:
    public List<LevelItemsConfiguration> levelItemsConfiguration;


    This script is in the main menu scene, but not in the level scenes where we are playing.
    This list have the properties of each level of the game stored (level name, level images, level locked or unlocked, etc). One of those properties tells you if the level is locked or unlocked.:
    levelItemsConfiguration[i].isLocked


    The only thing i want to do its to lock and unlock levels via code.
    I mean, do something like this:
    levelItemsConfiguration[UnlockLevelNumber].isLocked=false; 


    In my GameManager Script, which is in all the levels of the game (but not in the main menu scene where the asset and the scripts of this asset are), i am trying to do this:
    public LevelSelectManager levelSelectManager;
    LevelSelectManager levelSelectManager = GetComponent<LevelSelectManager>();
    levelSelectManager.levelItemsConfiguration[1].isLocked;


    This gives me this error:
    NullReferenceException: Object reference not set to an instance of an object 


    I think that the problem is that i am not assigning the correct game object in the inspector, but i dont know what to assign in the GameManager script, to the levelSelectManager object (i cant assign the LevelSelectManager script).

    Any ideas?

    Thank you!
     
  2. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    Do you have a LevelSelectManager script on the GameObject which has the GameManager script?

    Sam
     
  3. SoontirF

    SoontirF

    Joined:
    Apr 24, 2018
    Posts:
    7
    Hi Sam,

    Thank you for your answer.
    No, the LevelSelectManager script is in other scene (MainMenu scene, of the Easy Main Menu asset).
    That script is part of the Easy Main Menu Asset.
     
  4. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    Ah ok, that's probably why it's failing then. You are doing:
    GetComponent<LevelSelectManager>()
    but there is no
    LevelSelectManager
    on your game object. So you need to link up your game object to the
    LevelSelectManager
    somehow. Either set up a serialised or public field on your game object to link it up, or call
    FindObjectOfType<LevelSelectManager>()
    to get a reference to it.

    You'll need to ensure that the MainMenu scene is loaded at the same time as your scene with your GameManager object.

    Sam