# NullReferenceException (655548)

**URL:** <https://discussions.unity.com/t/nullreferenceexception-655548/655548>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [February 15, 2017, 11:55pm UTC](https://discussions.unity.com/t/nullreferenceexception-655548/655548 "2017-02-15T23:55:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlamingGenius](https://avatars.discourse-cdn.com/v4/letter/f/3ab097/32.png) [@FlamingGenius](https://discussions.unity.com/u/FlamingGenius)\
**Post date:** [February 15, 2017, 11:55pm UTC](https://discussions.unity.com/t/nullreferenceexception-655548/655548/1 "2017-02-15T23:55:17Z")

</div>

So i cant for the life of me figure out the issue

heres the error im getting  
Error in console

```csharp
NullReferenceException: Object reference not set to an instance of an object
GameController.UpdateInfo () (at Assets/newScripting/GameController.cs:265)
GameController.Update () (at Assets/newScripting/GameController.cs:174)

```

here is the line of code the error is pertaining to

```csharp
UpgradeManager.Instance.AddButton ("Upgrade Keyboard\n$" + keyboardCost, keyboardCost, "proKeyboard");

```

and here is the function thats being called  
function being called

```csharp
public void AddButton(string text,int cost,string type){
        Button[] buttons = this.GetComponentsInChildren<Button> (true);
        foreach (Button but in buttons) {
            if (but.GetComponent<ButtonInfo> ().buttonType == type) {
                return;
            }
        }
        GameObject b = Instantiate (buttonPrefab, new Vector3 (20, -bc * 50, 0), buttonPrefab.transform.rotation) as GameObject;
        b.transform.SetParent (transform, false);
        b.GetComponentInChildren<Text> ().text = text;
        b.GetComponent<Button> ().onClick.AddListener (delegate {
            HandleButton(type);
        });
        b.GetComponent<Button> ().onClick.AddListener (delegate {
            this.buttonPressed(b.GetComponent<Button>());
        });
        b.GetComponent<ButtonInfo> ().buttonCost = cost;
        b.GetComponent<ButtonInfo> ().buttonType = type;
        bc++;
        if (bc * 50 > 250) {
            GetComponent<RectTransform> ().sizeDelta = new Vector2 (237, bc * 50);
        } else {
            GetComponent<RectTransform> ().sizeDelta = new Vector2 (237, 250);
        }
    }

```

im not sure what the issue is i have the “buttonPrefab” as a public gameobject and i set it in the inspector

---

<div class="post-metadata">

**Author:** ![Antony-Blackett](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/antony-blackett/32/102413_2.png) [@Antony-Blackett](https://discussions.unity.com/u/Antony-Blackett)\
**Post date:** [February 16, 2017, 1:00am UTC](https://discussions.unity.com/t/nullreferenceexception-655548/655548/2 "2017-02-16T01:00:55Z")

</div>

If the problem was in AddButton then the error log would say it was inside AddButton. Seeing as the error log ends at UpdateInfo() then I can only assume that UpgradeManager.Instance is null.

Are you setting UpgradeManager.Instance to an instance anywhere? If you are is it possible that UpdateInfo is called before the Instance is set? Or is the Instance destroyed at any point, like at a scene load for example?

---

<div class="post-metadata">

**Author:** ![FlamingGenius](https://avatars.discourse-cdn.com/v4/letter/f/3ab097/32.png) [@FlamingGenius](https://discussions.unity.com/u/FlamingGenius)\
**Post date:** [February 16, 2017, 1:06am UTC](https://discussions.unity.com/t/nullreferenceexception-655548/655548/3 "2017-02-16T01:06:50Z")

</div>

you are my hero sir i forgot to set the instance in the start function cant believe i mised something so simple
