Object Reference not set to an instance of object

Hey guys, So im getting the error in the title.

I’m trying to create a numeric inventory system(for keys 1,2,3,4 and 5).

Here is my code.

using UnityEngine;
	using System.Collections;

	public class playerInventory : MonoBehaviour {
		public int currentWeapon;
		public GameObject[] weapons;
		public int selected = 0;

		// Use this for initialization
		void Start () {
		// Update is called once per frame
		void Update () {
			loopThroughBackpack ();

		void loopThroughBackpack(){
			if (Input.GetKeyDown (KeyCode.Alpha1)) {
				selected = 0;
				Debug.Log (weapons[selected]);
			else if(Input.GetKeyDown(KeyCode.Alpha2)){
				selected = 1;
				Instantiate (weapons[selected], weapons[selected].transform.parent.position, transform.rotation );




What im trying to do is spawn a asset called canteen and then parent it to the First person controller’s Main Camera.

Any help is really appreciated!

nowhere in that code have you initialised the weapons array.

at the very least you will need to do

GameObject[] weapons = new GameObject[10]; //or whatever your number of items is

and then you gotta put some stuff in there :smiley: