- Home /
Change sprite color on GameObjects from a list.
Hi, I'm trying to change the color on some sprites so they're transparent. I'm doing it with this code:
public List<GameObject> redFloorList;
for(int i = 0; i < redFloorList.Count; i++)
{
//Trying to change the color here
redFloorList[i].collider.isTrigger = false;
}
Does anyone know how to do this?
redFloorList[i].collider.isTrigger = false;
That will switch their colliders to not being triggers and does nothing with their appearance.
Answer by robertbu · Jul 13, 2014 at 02:33 PM
Try:
redFloorList[i].GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 0.35f);
Note that if any of the entries on the redFloorList are not sprites, this line will cause a null reference exception. If you want them to be completely transparent, then it would be better to disable the renderer:
redFloorList[i].renderer.enabled = false;
ok, it's only SpriteRenderers. What if I have 50 floor sprites and I run this on a phone, won't it lag?
I doubt it will lag, but if you are going to do it frequently, then create an initialize an array. An expensive part of the operation is the GetComponent(). You can do this once for each for and build an array of references so you don't have to do it every time you change the color.
Your answer
Follow this Question
Related Questions
Assigning current color to a variable for fade out (C#) 0 Answers
Argument out of range exception 1 Answer
How can i change the gameobject color based on RGBA 1 Answer
cant get characters to load 1 Answer
How to put gameObjects to the list? 4 Answers