0

I have an error:

NullReferenceException
Shop.MakeShopWindow (.PlayerWeapon _weapon, UnityEngine.UI.Button _btn) (at Assets/Scripts/Menu/Shop.cs:58)
Shop.Start () (at Assets/Scripts/Menu/Shop.cs:29)

I looked through the internet everywhere but I couldn't find it. I tried looking at all my code in three scripts which all are being used in my shop but I couldn't find anything. My main script for the in-game store Shop.cs:

using UnityEngine;
using UnityEditor;
using UnityEngine.UI;

public class Shop : MonoBehaviour {

    public PlayerWeapon[] weapons;

    public GameObject[] graphics;

    public Button[] buttons;

    public Button backButton;

    public int money;

    void Start()
    {
        addWeaponsToArray();
        backButton.onClick.AddListener(delegate ()
        {
            SwitchScene.MainMenu();
        });        
        for(int i = 0; i < weapons.Length; i++)
        {
            if (buttons[i] == null)
                Debug.LogError("Shop: No button referenced in screen for index " + i + ".");
            else
            {
                MakeShopWindow(weapons[i], buttons[i]);
                if (!weapons[i].purchased && weapons[i].cost > money || weapons[i].active)
                {
                    buttons[i].enabled = false;
                }
                buttons[i].onClick.AddListener(delegate ()
                {
                    if (weapons[i].purchased)
                    {
                        foreach(PlayerWeapon _weapon in weapons)
                        {
                            _weapon.active = false;
                        }
                        foreach (Button _btn in buttons)
                        {
                            _btn.enabled = true;
                        }
                        weapons[i].active = true;
                        buttons[i].enabled = false;
                    }
                    else if (weapons[i].cost <= money)
                    {
                        if (EditorUtility.DisplayDialog("Buy the " + weapons[i].name + "?", "Are you sure you would like to by the " + weapons[i].weaponName + " for " + weapons[i].cost + " points?", "YES! LET'S DO IT!", "no. maybe later"))
                        {
                            foreach (PlayerWeapon _w in weapons)
                            {
                                _w.active = false;

                            }
                            foreach (Button _btn in buttons)
                            {
                                _btn.enabled = true;
                            }
                            weapons[i].purchased = true;
                            weapons[i].active = true;
                            buttons[i].enabled = false;
                        }
                    }
                });
            }
        }
    }

    void MakeShopWindow(PlayerWeapon _weapon, Button _btn)
    {
        //Title
        GUIText title = new GUIText();
        title.text = _weapon.weaponName;
        title.fontSize = 50;
        title.transform.parent = _btn.transform;
        title.transform.position = new Vector3(0, 500);

        //Description
        GUIText description = new GUIText();
        description.text = _weapon.description;
        description.transform.parent = _btn.transform;
        description.transform.position = Vector3.zero;

        //Cost
        GUIText cost = new GUIText();
        cost.text = _weapon.cost.ToString();
        cost.transform.parent = _btn.transform;
        cost.transform.position = new Vector3(0, 500);
    }

    //Format:
    //weapons.setValue(CreateWeapon.newWeapon(<Name>, <Price>, <Description>, <Damage>, <Fire Rate>, <Inaccuracy>, [Range], [Purchased or not], [If it's active]), weapons.Length)
    void addWeaponsToArray()
    {
        weapons.SetValue(CreateWeapon.newWeapon("Rotofury", 1, "3 words: brute, forceful, and inacurate", 100, 0.77f, 50), 0);
    }

}

Then I have my script for creating weapons CreateWeaopn.cs:

using UnityEngine;

public class CreateWeapon {

    public static PlayerWeapon newWeapon(string _name, int _cost, string _description, int _damage, float _fireRate, int _inaccuracy, int _range = 100, bool _purchased = false, bool _active = true, GameObject _graphics = null)
    {
        PlayerWeapon _weapon = new PlayerWeapon();

        _weapon.weaponName = _name;
        _weapon.cost = _cost;
        _weapon.description = _description;
        _weapon.damage = _damage;
        _weapon.fireRate = _fireRate;
        _weapon.range = _range;
        _weapon.purchased = _purchased;
        _weapon.active = _active;
        _weapon.inaccuracy = _inaccuracy;
        _weapon.graphics = _graphics;

        return _weapon;
    }

}

and my weapon script PlayerWeapon.cs:

using UnityEngine;

public class PlayerWeapon : MonoBehaviour {

    public string weaponName;
    public int cost;
    public string description;

    public int damage;
    public float range;
    public float fireRate;

    public bool purchased;
    public bool active;

    public int inaccuracy;

    public GameObject graphics;

}

Here are a few pictures of my unity project:

The view of the game:
enter image description here

The elements in the game:
enter image description here

The part of the inspector for the Shop.cs is in (It's part of the Canvas' inspector):
enter image description here

To try to fix this, I tried copying all the code into a new script, and the same error happened.


salipshitz
  • 67
  • 13
  • Are you sure that weapon array is populated properly? – Valentin Apr 13 '16 at 16:11
  • To make this question to be not duplicate of basic "NRE&fix" one make sure to provide [MCVE] and clarify what steps you've already taken to find the problem. – Alexei Levenkov Apr 13 '16 at 16:11
  • The problem is that you populate your array using `weapons.SetValue(CreateWeapon.newWeapon("Rotofury", 1, "3 words: brute, forceful, and inacurate", 100, 0.77f, 50), 0);` It creates and update only a first element. – Valentin Apr 13 '16 at 16:13
  • so how would I fix it? – salipshitz Apr 13 '16 at 16:33

0 Answers0