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 RandomDeveloper · Mar 26, 2013 at 05:14 PM · arrayelement

getting variable from every element in array?

This is exactly what the title says. I have a array of a custom class I made called Spell. Spell has multiple variables in it and one of them is called Icon. I need to get the Icon (Which is a Texture2D) from all of the elements in a array called actions and display it as buttons in a GUI group. I will put the code bellow.

 var controlTexture : Texture2D;
 var texturetodisplay : Texture2D;
 var openPic : Texture2D;
 var Open : boolean;
 var Self : GameObject;
 var Class : PlayerClass;
 var Abilities : Spell;
 var AvaibleAbilities : Vector2;
 
 function OnGUI ()
 {
 if (GUI.Button (Rect (1300,850,200,200),controlTexture,"")){
         Open = true;
         }
         if(Open){
             GUI.DrawTexture(Rect(300,50,1200,800), openPic);
             GUI.DrawTexture(Rect(570,80,500,500), Class.type);
             GUI.DrawTexture(Rect (490,50,500,500),Class.Level);
             GUILayout.BeginArea(Rect(1100,620,1200,800));
             AvaibleAbilities = GUILayout.BeginScrollView(AvaibleAbilities, GUILayout.Width(70), GUILayout.Height(180));
             GUILayout.BeginVertical();
             GUILayout.Button(Class.actions.icon);
             GUILayout.EndVertical();
             }
             GUILayout.EndScrollView();
             GUILayout.EndArea();
         }
 
 
 function Start (){
 WaitForSeconds(1);
 Class = Self.GetComponent("HUD").ClassType;
 }

Then I have the Spell class :

 class Spell{
 
     var name : String;
     var type = "action";
     var icon : Texture2D;
     static var description : String;
     var Damage : int;
     var animation : AnimationClip;
     var Projectile : Transform;
     var Cooldown : int;
     var Level : int;
  
     // The generic use function for the action
     function use(){
         Debug.Log(description);                        // Do stuff
     }
 }

So is there any methods I can use to do this?

Comment
Add comment · Show 1
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 Josh707 · Mar 26, 2013 at 05:44 PM 0
Share

Seems like a simple for loop using the index on the array would work if you use Actions[i].icon for the GUIContent. Im on my phone but if you want a code example I wouldnt $$anonymous$$d helping

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by iwaldrop · Mar 26, 2013 at 05:51 PM

You can use a for or a foreach loop:

 for (int i = 0; i < array.Length; i++)
     Debug.Log(array[i]);
 
 foreach (object o in array)
     Debug.Log(o);
Comment
Add comment · Show 13 · 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 RandomDeveloper · Mar 27, 2013 at 07:14 PM 0
Share

I have changed my code to this:

GUILayout.BeginArea(Rect(1100,620,1200,800)); AvaibleAbilities = GUILayout.BeginScrollView(AvaibleAbilities, GUILayout.Width(70), GUILayout.Height(180)); for (var i = 0; i < Class.Actions.Length; i++) GUILayout.BeginVertical(); if(GUILayout.Button(Class.Actions[i].icon)); GUILayout.EndVertical(); } GUILayout.EndScrollView(); GUILayout.EndArea(); }

But I get a massive amount of errors. How would the loop look in my code?

avatar image iwaldrop · Mar 28, 2013 at 03:08 AM 0
Share

A loop would look just like the code I posted above. I don't know what you're trying to loop through however, because there aren't any collections in your code example. I also don't know where your errors are co$$anonymous$$g from, because you haven't formatted your code. Use the '101-010' button to format it so that it's readable, and I'll take another look at where your errors are co$$anonymous$$g from.

As for how your code might look with a loop, could you share what you're trying to loop through?

avatar image RandomDeveloper · Mar 28, 2013 at 04:21 PM 0
Share

I get how this is really confusing. I have another class called PlayerClass which has a array of spells inside it called Actions. So now I have Class which is a PlayerClass and it has three Actions (Spells in actions) that I set through the inspector. Now all I need to do is to display the icon of all the Actions on line 24. I just have no idea what line the foreach loop should be on.

avatar image iwaldrop · Mar 28, 2013 at 04:49 PM 0
Share

Basically, you'll want to do the following:

 for (int i = 0; i < Actions; i++)
     GUI.Box(new Rect(xPos * i+1, yPos, boxWidth, boxHeight), Actions[i].guiContent);

You just specify an x and y value somewhere (starting where you want the first block placed) and draw each block at a location at a position defined by x i+1. That means that if you start at an x position of 50 then the first block is placed at 50, whereas the second block would be at 100 (x 1+1 = 50 * 2).

$$anonymous$$ake sense? It's really quite trivial to do, you just need to work out the values that you want to use as spacing.

avatar image RandomDeveloper · Mar 28, 2013 at 05:19 PM 0
Share

I do have something set up for the button but I used your example ins$$anonymous$$d. I can post the code and all the errors I have. I can send you TONS of my code and you can see why I get errors. I still get a bunch and nothing displays :(.

 function OnGUI ()
 {
 if (GUI.Button (Rect (1300,850,200,200),controlTexture,"")){
         Open = true;
         }
         if(Open){
             GUI.DrawTexture(Rect(300,50,1200,800), openPic);
             GUI.DrawTexture(Rect(570,80,500,500), Class.type);
             GUI.DrawTexture(Rect (490,50,500,500),Class.Level);
             GUILayout.BeginArea(Rect(1100,620,1200,800));
             AvaibleAbilities = GUILayout.BeginScrollView(AvaibleAbilities, GUILayout.Width(70), GUILayout.Height(180));
             for (var i = 0; i < Class.Actions.Length; i++)
             GUILayout.BeginVertical();            
             GUI.Box(new Rect(500 * i+54, 600, 200, 200), Class.Actions[i].guiContent);
             GUILayout.EndVertical();
             }
             GUILayout.EndScrollView();
             GUILayout.EndArea();
         }

Thats my code now and heres the errors I get.

IndexOutOfRangeException: Array index is out of range. SpellBook.OnGUI () (at Assets/Accounts/Player/GUI/SpellBook.js:25)

NullReferenceException UnityEngine.Texture.get_width () UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, Scale$$anonymous$$ode scale$$anonymous$$ode, Boolean alphaBlend, Single imageAspect) UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image) SpellBook.OnGUI () (at Assets/Accounts/Player/GUI/SpellBook.js:20)

If you want I can show you my HUD script and my PlayerClass script.

Show more comments

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

12 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

Related Questions

how subtract total value element of array ?? 1 Answer

How to see what element is the current one 2 Answers

problem adding element of array 1 Answer

How do i access the variables of a class that is in an array? 1 Answer

how to check if an element has been removed from a GameObject array 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