- Home /
Assigning GUITexture through Javascript
Hello,
I've been trying to get a bit of code working. below is a truncated version for demonstration purposes. I'm trying to figure out how to store an array of GUITextures, that I can then access values to. I've banged my head against this all day, and I have not found a very good example online of how this is achieved. Here is what I am trying to do.
I have been able to create the array, but not populate it. Since, I have not achieved that I also do not know if the color assignment, position and scale variables have been set. I think those should work, but then a again, I must be missing something fundamental. Is GUITextures just not intended to be scripted? I find that hard to believe. If anyone has any ideas I would love to hear them.
Best Regards,
Stephen
var numberOfButtons:int = 7;
var buttonBGArray = new GUITexture[numberOfButtons];
for(var x:int = 0; x < numberOfButtons; x++)
{
var bgColor:Color;
bgColor = Color(1,0,0,.5);
buttonBGArray[x].texture = textureArray[categoryTextureNumber];
buttonBGArray[x].color = bgColor;
buttonBGArray[x].transform.position = Vector2(xPlacement,yPlacement);
buttonBGArray[x].transform.localScale = Vector2(buttonWidth,buttonHeight);
}
Sure thing. "textureArray" is a Texture2D[] Array that I created where I store all image files that are going to be used in the application. These are loaded from the Resources directory.
I also have a Hashtable that is populated from an X$$anonymous$$L file that is parsed. This Hashtable contains variables and properties including texture information. "categoryTextureNumber" is a value that associates the the textureArray to the Hashtable. $$anonymous$$inda hard to explain actually.
Ultimately I am trying to associate a Texture2D with the GUITexture .texture variable.
That sounds pretty straightforward.
Are you sure that these objects are correctly initialized?
I don't see anything wrong with your code, so I would start with some debug statements.
First I would make sure that textureArray[categoryTextureNumber] returns a valid Texture by printing some simple imformation to the console.
I would also add a "test" for loop right after you're done populating the buttonBGArray to make sure the first for loop worked. Something as simple as this:
for ( var thisButt : GUITexture in buttonBGArray ) {
Debug.Log("color: " + thisButt.color);
}
If that doesn't work then you know your problem is somewhere else.
Sorry, not trying to be insulting.
It's just that your code looks good and you don't show any of your other code, so there's a chance that the error could be somewhere else.
Answer by aldonaletto · Oct 12, 2011 at 05:11 AM
The array buttonBGArray only holds the references, not the GUITextures themselves. I suspect that for each element you must create an empty GameObject and add to it a GUITexture with AddComponent:
for(var x:int = 0; x < numberOfButtons; x++) { var gObj = new GameObject("GO"+x); // create the game object gObj.AddComponent("GUITexture"); // add the GUITexture... buttonBGArray[x] = gObj.guiTexture; // and save its reference buttonBGArray[x].texture = ... ...
That is really cool. It worked like a charm. $$anonymous$$any, many thanks! So it looks like all of the properties like Color, position, etc. are able to be assigned once the GUITexture is referenced to the Array. Pretty powerful stuff. Thank you again for a point in the right direction!
Your answer
![](https://koobas.hobune.stream/wayback/20220613004656im_/https://answers.unity.com/themes/thub/images/avi.jpg)