- Home /
Pushing textures in an Array
I am trying to push Texture2D into an array whose length is determined by a variable, to then display as GUI labels. I must be doing something wrong as I get a "null" when checking if my texture has been pushed in the array. I'm relatively new to scripting as well. Here is my code:
var numberOfHits = 5;
var emptySlots : Texture2D;
var emptySlotsArray = new Array();
emptySlotsArray.length = numberOfHits;
var buttonQTEWidth = 65;
var buttonQTEHeight = 50;
var buttonQTESpacing = 6;
var screenAdjust : float = 1.3;
function OnGUI () {
for (i = 0 ; i < numberOfHits ; i ++)
{
emptySlotsArray.Push (emptySlots);
GUI.Label(Rect (Screen.width / 2 - (buttonQTEWidth * numberOfHits) / 2 + (i * buttonQTEWidth), Screen.height / screenAdjust - buttonQTEHeight /2 , buttonQTEWidth, buttonQTEHeight), emptySlotsArray[i]);
}
}
Thanks for your help.
Answer by rutter · Mar 26, 2012 at 06:42 AM
Try commenting out this line:
emptySlotsArray.length = numberOfHits;
I'm wondering if setting the array's length might leave you with a bunch of empty entries.
If my hunch is right, your Push()
calls are adding more entries past the empty ones that are already present. Easy enough to solve by avoiding those empty entries to begin with.
Answer by Wild Pegasus · Mar 27, 2012 at 05:26 PM
I have tried doing that but I kept getting a null. Is there a way of pushing textures in an array that I might be doing wrong?
Now that you mention it, there is one more thing:
var emptySlots : Texture2D;
Do you ever assign that value? You could grab a texture using `Resources.Load`, or make it a public variable that you can set from the inspector.