NullReferenceException on getComponent
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[i] != null) //if there is an item added to the list
             {
                 Item duplicate = list[i].GetComponent<Item>(); 
                 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[i].GetComponent<Item> ().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?
Answer by btmedia14 · Aug 29, 2016 at 11:56 PM
The conditional expression requires a == rather than =. Try replacing the conditional check
  if(duplicate = null) 
with
  if(duplicate == null) 
Your answer
 
 
             Follow this Question
Related Questions
How to alter Right/Top Value from RectTransform 0 Answers
Adding a cost reduction to my purchasable items 1 Answer
Having trouble deleting objects from a list after they reach a certain scale. 0 Answers
How attach C# script by argument? 3 Answers
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                