- Home /
I need to know why I'm getting a null reference exception
I need to know why I'm getting a null reference exception in my method. Here's the method:
public Texture2D lootRandomizer()
{
cItemClass item = new cItemClass();
Texture2D returnString = item.eEmptyIcon;
int randomNumber = Random.Range(0, 2);
switch (randomNumber)
{
case 0:
Debug.Log(randomNumber.ToString());
returnString = item.ehealingPotionIcon;
Debug.Log(returnString.ToString());
break;
case 1:
Debug.Log(randomNumber.ToString());
returnString = item.eswordIcon;
Debug.Log(returnString.ToString());
break;
case 2:
Debug.Log(randomNumber.ToString());
returnString = item.egunIcon;
Debug.Log(returnString.ToString());
break;
default:
returnString = item.healingPotion.icon;
break;
}
return returnString;
}
The item class its referencing to set the icons to is here:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class cItemClass : MonoBehaviour
{
public Texture2D eEmptyIcon;
public Texture2D eswordIcon;
public Texture2D ehealingPotionIcon;
public Texture2D egunIcon;
// icons for the GUI
static public Texture2D emptyIcon;
static public Texture2D swordIcon;
static public Texture2D healingPotionIcon;
static public Texture2D gunIcon;
//Create any new items here to be used
public ItemCreatorClass healingPotion = new ItemCreatorClass(0, "HealingPotion", healingPotionIcon, "A potion that heals 25 Health points");
public ItemCreatorClass sword = new ItemCreatorClass(1, "Sword", swordIcon, "A Sword to kill with");
public ItemCreatorClass gun = new ItemCreatorClass(2, "Gun", gunIcon, "A gun to kill with");
void Start()
{
emptyIcon = eEmptyIcon;
swordIcon = eswordIcon;
healingPotionIcon = ehealingPotionIcon;
gunIcon = egunIcon;
}
void Update()
{
}
public class ItemCreatorClass
{
public int Id;
public string name;
public Texture2D icon;
public string description;
public ItemCreatorClass(int id, string n, Texture2D t, string d)
{
Id = id;
name = n;
icon = t;
description = d;
}
}
}
You should post the stack trace from the Unity Console so we know exactly what line of code is causing it.
I can post the entire chestItems class if needed.Its the lines that return the texture2d so in the above code lines 10,15,20,or 24.
This is the error code: NullReferenceException: Object reference not set to an instance of an object ChestItems.lootRandomizer () (at Assets/Resources/Scripts/ChestItems.cs:49) ChestItems.Start () (at Assets/Resources/Scripts/ChestItems.cs:76)
Answer by robertbu · May 17, 2014 at 06:09 AM
'cItemClass' is derived from MonoBehaviour. You are creating an instance of that class using the 'new' operator. You cannot use 'new' with a MonoBehaviour-derived class. If you want to dynamically create this class, you'd have to add a constructor to 'lootRandomizer' and then do something like:
GameObject go = new GameObject();
item = go.AddComponent<cItemClass>();
Alternately you could elect to not derive the class from MonoBehavior().
As I look at your code, you create the cItemClass dynamically, but nowhere do I see the initialization of these public variables:
public Texture2D eEmptyIcon;
public Texture2D eswordIcon;
public Texture2D ehealingPotionIcon;
public Texture2D egunIcon;
If there is initialization code somewhere, it is likely not getting called due to the use of 'new' with a MonoBehaviour.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145224im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
NullReferenceException script problem? 0 Answers
[Roll-a-ball] Changing UI text (NRE) 0 Answers
How do i make sure an Object is initialized before using it ? C# 2 Answers