I have assigned a separate script called “Item” with a few variables and integers that I would like to call, however using getcomponent I get a reference exception.
using UnityEngine;
using System.Collections;
public class ItemList : MonoBehaviour
{
public GameObject[] list = new GameObject[100];
void Start ()
{
for(int i = 0; i < 100; i++)
{
if(list *!= null) //if there is an item added to the list*
-
{*
_ Item duplicate = list*.GetComponent();_
_ if(duplicate = null) //checks if the object has the “Item” script attached*_
* {*
* print (“Object " + i + " Does not have Item script attached”);*
* }*
* else*
* {*
_ string str = list*.GetComponent ().getName(); //acesses method within “Item” class.
print ("Found " + str);
}
}
}*_
* }*
}
NullReferenceException: Object reference not set to an instance of an object
ItemList.Start () (at Assets/Scripts/Inventory/ItemList.cs:21)
I know for sure that the object was added to the list in the inspector, and I only got an error the second time I called GetComponent. Does anyone know what the issue is?