- Home /
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?
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
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);
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?
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?
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.
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.
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.