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 Kiwuser · Dec 26, 2013 at 10:28 PM · class objectreading datagui-problemunity4.0 problem

[solved] Problem getting data from array

I am trying to make an inventory system for my game, but I have a problem, I have an array of Item classes, which hold the data for the inventory, but for some reason when I try to read it to get the icon of the object for use in the GUI, the function ignores everything and treats it as null, I know that the array isn't null, because when an object is picked up, Debug.Log can display the name variable stored in the Item class.

This is the function that reads the array and creates a set of GUILayout.boxes for the items:

 GUILayout.BeginVertical(GUI.skin.box, GUILayout.Width(38));
             for(int i=0; i<10; i++)
             {
                 Item itm = getItemFromSlot(i);
 
                 if(itm != null)
                 {
                     GUILayout.Box(itm.icon, GUILayout.Width(32), GUILayout.Height(32));
                 }
                 else
                 {
                     GUILayout.Box("", GUILayout.Width(32), GUILayout.Height(32));
                 }
             }
             GUILayout.EndVertical();

And the getItemFromSlot function:

 static Item getItemFromSlot(int slot)
     {
         return items[slot];
     }

And lastly the Item class:

 using UnityEngine;
 using System.Collections;
 
 public class Item : MonoBehaviour
 {
 
     public string type;
     public string objName;
     public Texture2D icon;
     public GameObject obj;
 
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "Player")
         {
             if(MainGUIHandler.addItem (this))
                 Destroy (gameObject);
             else
                 Debug.Log("Failed to add");
         }
     }
 
 }

I have no idea why the function would return nothing and leave blank boxes every time, the array has objects in it.

Comment
Add comment · Show 9
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 T27M · Dec 26, 2013 at 11:31 PM 0
Share

Your loop runs 10 times do you have at least 10 slots in the array? When you say you have an array of "item classes", does the class correspond to an item?

You might be better with a List since they are more flexible. Also running loops in OnGUI can cause issues, try to keep as much logic as possible out of OnGUI or avoid OnGUI if possible.

avatar image fafase · Dec 27, 2013 at 12:09 AM 0
Share

The whole GUI thing is the OnGUI right? T27$$anonymous$$, why is a loop causing issues in OnGUI?

avatar image Benproductions1 · Dec 27, 2013 at 02:01 AM 0
Share

Are you getting any errors? If so you should post them in the question

avatar image sparkzbarca · Dec 27, 2013 at 02:09 AM 0
Share

well you need to start using debug.logs and making arrays public so you can see the values in the editor.

For example i'm not sure if an "empty" array thing is actually null, it might be filled with a default object or just junk.

do this

Item itm = getItemFromSlot(i);

debug.log (itm);

you should probably just use a list ins$$anonymous$$d.

avatar image T27M · Dec 27, 2013 at 02:09 AM 0
Share

Sorry, I wasn't clear enough. It was my understanding that running loops in OnGUI can result in performance issues. Yes,No?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

If use Contructor in my program Contructor is calling two times,If I use Constructor in my Program Constructor is calling 4 times why? 0 Answers

can i find Joystick Position based on screen size? 0 Answers

Reading NFC Tags on Android 0 Answers

Why is Unity freezing with this code? 1 Answer

"NullReferenceExecption: Object reference not set to an instance of an object" error when using array of own made class objects. 1 Answer


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