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 21, 2018 at 05:36 PM · arraysscriptableobjectui image

Trying to assign UI images from an array with scriptable objects not working?

So I am making an armor system, and so when the player equips a new peice of armor, it goes into the public armor array that can be acessed anywhere. In order to show it visually, I made a script that should always be setting each respective 'box' in your inventory to that armor peice to show you have equiped it... but of course Im getting an error that says object refrence not set to an instance of an object. Im not sure what isn't defined here.. the aray is full of scriptable 'items' that have an icon, and I know that it can work because without all the if else statements, I tried setting it manually and the image changed correctly, but it still gave me an error despite working fine. I am very confused. Any ideas?

Here is the script that manages the UI of the armor:

 public Image Head;
 public Image Body;
 public Image Legs;
 public Image Charm;
 //public Image Weapon;
 public Image tets;

 EquipmentManager equipManager;
 

 void Start()
 {
     equipManager = EquipmentManager.instance;
 }

 void Update()
 {

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

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

 }

Here is the script with the equipment array in it:

 #region singleton
 public static EquipmentManager instance;

 void Awake()
 {
     instance = this;
 }

 #endregion


 public Equipment[] currentEquipment;



 Inventory inventory;

 void Start()
 {
     inventory = Inventory.instance;

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

 }

 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;
 }

}

Comment
Add comment · Show 10
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 ecv80 · Nov 21, 2018 at 11:28 PM 0
Share

Can you paste the exact error?

avatar image ragefordragons ecv80 · Nov 22, 2018 at 12:19 AM 0
Share

The Exact Error is:

NullRefrenceExeption: Object reference not set to an instance of an object EquipmentUI.Update() (At Assets/EquipmentUI.cs 26)

avatar image ecv80 ragefordragons · Nov 22, 2018 at 08:58 AM 0
Share

Can you paste the line the error is referring to? If it's this one: Head.enabled = false; then Head was not set before that line. Did you actually set it in the inspector? Silly questions, I know. Also could you paste the contents of Equipment class in case?

Show more comments
avatar image ecv80 · Nov 21, 2018 at 11:51 PM 0
Share

This will still fail, but it as long as you don't intend to make big changes per different parts, might be a bit cleaner:

 for (int i=0; i<equip$$anonymous$$anager.currentEquipment.Length; i++) {
     Image part;
     switch(i) {
         case 0:
             part=Head;
         break;
         case 1:
             part=Body;
         break;
         case 2:
             part=Legs;
         break;
         case 4:
             part=Charm;
         break;
         default:
             continue;
         break;
     }
     
     if (equip$$anonymous$$anager.currentEquipment[i].icon != null)
      {
          part.enabled = true;
          part.sprite = equip$$anonymous$$anager.currentEquipment[i].icon;
      }
      else
          part.enabled = false;
 }


Also, it might be more efficient to change these from the Equipment$$anonymous$$anager than checking constantly in this Update().

avatar image ragefordragons ecv80 · Nov 22, 2018 at 12:23 AM 0
Share

This still threw the same error, so I am starting to think it may be something to do with the equipment array itself. Im going to put that code on the question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Matthew_Ostil · Nov 21, 2018 at 06:55 PM

Try moving your caching of the EquipmentManager into the Awake method:

 void Awake()
  {
      equipManager = EquipmentManager.instance;
  }
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 21, 2018 at 11:03 PM 0
Share

Yeah, that didnt work...

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

100 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

Related Questions

Get fill amount up and down in a partern 0 Answers

Choose 4 random Images from a Sprite array and load them in premade Image containers without repeat selections? 1 Answer

Is there a way to optimize working with arrays? 3 Answers

Unable to serialize my list in a Unity Custom Editor script. 1 Answer

Array of scriptable Objects inside scriptable Object reseting on Play 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