Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by joshbarnettlloyd · May 17, 2014 at 04:34 AM · c#nullreferenceexceptionnull exception

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;
          }
 
     }
 
 }
 
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rgowen · May 17, 2014 at 05:38 AM 0
Share

You should post the stack trace from the Unity Console so we know exactly what line of code is causing it.

avatar image robertbu · May 17, 2014 at 05:38 AM 0
Share

You need to tell us what line is generating the error.

avatar image joshbarnettlloyd · May 17, 2014 at 05:51 AM 0
Share

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)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges