- Home /
How To Change Multiple GameObject's Textures
Hello, I have alot of objects that I want to change the texture of in runtime, instead of referencing each one. I first tried to use a tag to achieve this by using this script;
var yourNewTexture : Texture2D;
var ball = GameObject.FindGameObjectsWithTag("Ball");
function OnMouseDown() {
ball.renderer.material.SetTexture("_MainTex", yourNewTexture);
}
but I get this error BCE0019: 'renderer' is not a member of 'UnityEngine.GameObject[]'.
Secondly I tried to use shared material to change the material that affects the balls, using this script;
var yourNewTexture : Texture2D;
var similarObject : GameObject;
function OnMouseDown() {
similarObject.renderer.sharedMaterial.SetTexture("_MainTex", yourNewTexture);
}
When I used this script, all the balls change to the new color, but when I go out of play in the editor, the balls are permanently set to that color. For some reason the shared material keeps the change no matter what afterwards.
How would i either fix the first script (preferable for me) or make the second script not keep the changes after exiting the game? Thanks in advance for any help!
Answer by flamy · May 18, 2012 at 06:48 PM
var ball = GameObject.FindGameObjectsWithTag("Ball");
according to this line ball is a not a game object, it is an array of gameObjects, since FindGameObjectsWithTag returns an array instead of single element. So your code should be
var yourNewTexture : Texture2D;
var ball = GameObject.FindGameObjectsWithTag("Ball");
function OnMouseDown() {
for(int i=0;i<ball.Length;i++)
{
ball[i].renderer.sharedMaterial.SetTexture("_MainTex", yourNewTexture);
}
}
C# Version:
public Texture2D yourNewTexture;
public GameObject[] ball;
void Start()
{
ball = GameObject.FindGameObjectsWithTag("Ball");
}
void OnMouseDown() {
for(int i=0;i<ball.Length;i++)
{
ball[i].renderer.sharedMaterial.SetTexture("_MainTex", yourNewTexture);
}
}
if only one object in scene has ball tag use FindGameObjectWithTag so you will get a single game object instead of array. in this case you dont have to change you code except removing an 's'!!
lol i saw this for sometime and w8ed someone to post, since it is so simple :P but none did for 2 or 3 $$anonymous$$ so i posted ;) btw ya touche
@flamy hahaha, i did the same exact thing. Too funny
So I tried this, had to fix some things that the editor was complaining about, but pretty much the same. I get a few errors when I try though. First I get "You are not allowed to call this function when declaring a variable." then "FindGameObjectsWithTag can only be called from the main thread." In the end it gives me an array list that if I drag and drop the objects seperately it will change them but ignore if they are tagged or not, Is there something I'm missing?
Edit: Never$$anonymous$$d I got it to work with your help, thank you :)
Answer by hijinxbassist · May 18, 2012 at 06:48 PM
The first method doesnt work because you are basically saying
GameObject[].renderer....
An array does not have a renderer, each game object does however. So instead you need to call each of the objects individually.
var yourNewTexture : Texture2D;
var ball = GameObject.FindGameObjectsWithTag("Ball");
function OnMouseDown() {
for(var i:int=0;i<ball.length;i++)
{
ball[i].renderer.mainTexture=youNewTexture;
}
}
I switch from shared material, because as you saw before, this changes the texture in the assets folder permanently. Instead change main texture on the renderer.
Answer by Brizz104 · Nov 12, 2012 at 05:07 PM
Can I please get a c# version of this? The only hangup I'm having is on var ball....don't know that to call it in c#. Thanks!
ah thank you! didn't even know that GameObject[] existed!