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*.icon for the GUIContent. Im on my phone but if you want a code example I wouldnt mind helping*

1 Answer

1

You can use a for or a foreach loop:

for (int i = 0; i < array.Length; i++)
    Debug.Log(array*);*

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*.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 coming 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 coming 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.

You are missing the { and } around whatever is in that for(var i = 0; loop. I'm guessing that it should be around the begin and end verticals.

What whydidoit said, and you I think you should be creating the vertical group outside of the loop. Since you're using guilayout, you could also do a foreach. You also don't need to explicitly place the box; in other words you should just use GUILayout.Box(). :)