Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ragefordragons · Nov 26, 2018 at 02:16 AM · uiarrayscriptableobject

Referencing an array but its returning a nullrefrence exception?

So this one is really strange... I am attempting to create an armor system that shows up visually, and to do so I have this script that stores what armor is equipped (In an array public currentEquipment[])and then sets the images to the corresponding icons of the items in the array. (The objects in the array are scriptable "equipment" objects. And while this code lets the game continue running, it spits out a nullrefrence exeption every frame on all the armor images in the UI that dont have a armor object assigned to them. (The 'equipment' items all have a sprite associated with them that can be referenced other places just fine.) I am very very confused and I have no idea why 1.) Im getting these errors, 2.) why the game is still running despite these errors. (Ps. the error is on every line of void Update() where currentEquipment[] is called.)

Here is the code:

 #region singleton
 public static EquipmentManager instance;

 void Awake()
 {
     instance = this;
 }

 #endregion


 public Equipment[] currentEquipment;

 public Image Head;
 public Image Body;
 public Image Legs;
 public Image Charm;

 Inventory inventory;

 void Start()
 {
     inventory = Inventory.instance;

     int numSlots = System.Enum.GetNames(typeof(EquipmentSlot)).Length;
     currentEquipment = new Equipment[numSlots];

     Head.enabled = false;
     Body.enabled = false;
     Legs.enabled = false;
     Charm.enabled = false;

 }

 public void Equip(Equipment newItem)
 {
     int slotIndex = (int)newItem.equipSlot;

     Equipment oldItem = null;


     if(currentEquipment[slotIndex] != null)
     {
         oldItem = currentEquipment[slotIndex];
         inventory.Add(oldItem);
     }

     currentEquipment[slotIndex] = newItem;
 }

 void Update()
 {
     if (currentEquipment[0].icon != null)
     {
         Head.enabled = true;
         Head.sprite = currentEquipment[0].icon;
     }
     else
     {
         Head.sprite = null;
         Head.enabled = false;
     }

     if (currentEquipment[1].icon != null)
     {
         Body.enabled = true;
         Body.sprite = currentEquipment[1].icon;
     }
     else
     {
         Body.sprite = null;
         Body.enabled = false;
     }
     if (currentEquipment[2].icon != null)
     {
         Legs.enabled = true;
         Legs.sprite = currentEquipment[2].icon;
     }
     else
     {
         Legs.sprite = null;
         Legs.enabled = false;
     }
     if (currentEquipment[4].icon != null)
     {
         Charm.enabled = true;
         Charm.sprite = currentEquipment[4].icon;
     }
     else
     {
         Charm.sprite = null;
         Charm.enabled = false;
     }

 }

}

Comment
Add comment
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

2 Replies

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

Answer by s_awali · Nov 26, 2018 at 10:09 AM

No wonder your code throw a couple of NullPointerException, let me explain why:

  1. Your Equip() function is never called in your code, so your currentEquipment is always empty.

  2. Even if you add equipment in this array via the inspector, it is wiped out on startup, because you call currentEquipment = new Equipment[numSlots]; in OnStart() method.

  3. You do not do enough controls in your tests: if (currentEquipment[0].icon != null) should be if (currentEquipment[0] != null && currentEquipment[0].icon != null) to make sure there is an equipment in that specific array index.

Waiting for your feedback :)

NB: Oh and by the way, only your script crash, not the whole game, that's why it still run!

Comment
Add comment · Show 1 · 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
avatar image ragefordragons · Nov 27, 2018 at 12:56 AM 0
Share

Thanks, that fixed it. I guess I thought it was unusual for so many errors to not be effecting the game.

avatar image
0

Answer by ahstein · Nov 26, 2018 at 06:45 AM

I think you probably need to check that currentEquipment[0] != null before you check that currentEquipment[0].icon != null.

In response to #2: Errors don't necessarily make your game crash. There's a setting that makes the game pause when you get an error--you may have disabled it.

Comment
Add comment · Show 1 · 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
avatar image ragefordragons · Nov 27, 2018 at 12:57 AM 0
Share

Thanks a ton, this was correct but I can only accept one answer and I just happened to read the second one first.

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

175 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 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 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 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 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 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 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 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

Specify from inspector which scriptable field to use 0 Answers

How to Find the Sum of each Individual item / array out of range 0 Answers

Can't open dialogue box when pressing button 0 Answers

How can I instantiate parented ui objects above previously instantiated children? 1 Answer

GUI On button click change visibility of array objects 0 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