Loading different levels if inventory item is true

Hi, in my scene Im trying to load different levels on GUI depending on wether or not an object has been picked up.

Here’s my script:

public class Guibutton4 : MonoBehaviour {

    public Texture btnTexture;
    public GameObject Inventory;
    private AsylInventory asylInventory;

    void awake(){
        Inventory = GameObject.FindGameObjectWithTag ("Inventory");

        asylInventory = Inventory.GetComponent<AsylInventory>();

        }

    void OnGUI() {


        if (GUI.Button(new Rect(20, 10, 200, 30), "Return to cell")){

            if (asylInventory.GetComponent<AsylInventory>()){
                asylInventory.hasPick = true;


            Application.LoadLevel("AsylumCell1");
            }
       
            if (asylInventory.GetComponent<AsylInventory>()){
                asylInventory.hasPick = false;
                Application.LoadLevel("AsylumCell M");

    }
}
}
}

I have an empty game object with an inventory script called “AsylInventory” with a bool that becomes true when the object is picked up.
I want the level called “AsylumCell1” to load on GUI when the “has Pick” bool is true in the “AsylInventory” script and the level “AsylumCell M” to load on GUI when the “has Pick” bool is false in the “AsylInventory” script.

Simple right?
But with this script I keep getting the error “Object Reference not set to an instance of an object” and when I double click the error it highlights this line: “if (asylInventory.GetComponent()){”

I can’t figure out why, can anyone help me please?

if those are meant to be a check to see if the thing exists you just need

if(asylInventory) { ...

otherwise you are trying to get the component script you’ve already got via line 11

“FindGameObjectWithTag” this will find the first gameobject in the scene with that tag. I’m guessing there is more than one Inventory tagged gameobject in the scene and one doesn’t have the AsylInventory script on it.

oh and I’m going blind:

Awake()

not

awake()

lowercase function name isn’t the one the unity engine is looking for and it isn’t going to be called so asylInventory is never set to anything

OMG Fantastic thank you so much. :smiley: I’ve been staring at it trying all kinds of things all day and you where right!! It was another case of the pesky lower case letter issue. I’ve done that hundreds of times and I still do it from time to time. Can’t believe it :slight_smile: Well thanks again man you just made my day!