- Home /
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;
Debug.Log(weapons[selected]);
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!
Have you tried looping through the array to check for null references? - there could be a problem with the way you set it up
Have you assigned GameObjects to the weapons array? Especially to weapons[1]?
$$anonymous$$arcel is probably right. In arrays your first item is 0, second is 1, so on. You are trying to switch from 0 to 1. If youre getting a null reference you need to assign another item to your array to take the "1" slot. To test this you could just place the same item in the "1" slot (so you have 2 canteens) and your debug.log messages would ensure your code is working
Does public GameObject[] weapons; hold prefabs?
If it does I don't think this line is correct
weapons[selected].transform.parent.position
The error you are getting should contain a line number to indicate exactly where the npe happens. From that it should be pretty simple to track back what is left unassigned...
Answer by bubzy · Oct 22, 2014 at 09:09 PM
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 :D
So i put that in my code, Now how do i add objects too it?
If it is a public array you plan to hold prefabs you don't have to declare the size
Go to the editor
select the component with the script
you should see weapons variable expand it
then give prefab count in size and drag prefabs to the open slots
As an example of wijesijp's suggestion, the following code would also work without having to initialise in the script (if you set it up visually)...
using UnityEngine;
using System.Collections;
public class TestByOnlySettingInInspector : $$anonymous$$onoBehaviour {
public GameObject[] iSetThisArrayElementSizeAndObjectsInTheInspector;
// Update is called once per frame
void Update () {
if (iSetThisArrayElementSizeAndObjectsInTheInspector != null)
{
if (iSetThisArrayElementSizeAndObjectsInTheInspector.Length > 0)
{
for (int i = 0; i < iSetThisArrayElementSizeAndObjectsInTheInspector.Length; i++)
{
Debug.Log("I still work was called");
Debug.Log(iSetThisArrayElementSizeAndObjectsInTheInspector[i].name);
}
}
}
}
}
This does mean however you need to remember to setup via the inspector each time you add the script somewhere else. bubzy's suggestion means the array size is set for you, but you still then need to setup the GameObjects visually (as explained by wiksijp) or via script using:
GameObject.Find("NameOfObjectInHierarchy");
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Inventory "Remove" Function Help 1 Answer
How to ignore base class? 1 Answer
Can't Assign Item In Array 1 Answer