# Assigning GUITexture through Javascript

**URL:** https://discussions.unity.com/t/assigning-guitexture-through-javascript/29931
**Category:** Questions & Answers
**Created:** [October 12, 2011, 1:52am UTC](https://discussions.unity.com/t/assigning-guitexture-through-javascript/29931 "2011-10-12T01:52:06Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [October 12, 2011, 1:52am UTC](https://discussions.unity.com/t/assigning-guitexture-through-javascript/29931/1 "2011-10-12T01:52:06Z")

</div>

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);
}

```

---

<div class="post-metadata">

### Author: ![aldonaletto](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aldonaletto/32/44226_2.png) [@aldonaletto](https://discussions.unity.com/u/aldonaletto)
#### Post date: [October 12, 2011, 5:11am UTC](https://discussions.unity.com/t/assigning-guitexture-through-javascript/29931/2 "2011-10-12T05:11:29Z")

</div>

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 = ...
    ...
```
