I resolved my own issue
Change color of cloned prefab objects
I am trying to create a grid of tiles and basically have them all different colors(I have an array of 4 colors to choose from). I have been working on it for hours and looking online and nothing I do seems to make it change colors from the default prefab color.
I have a script that is creating the grid and the tiles. I have tried a few different ways(both are there, one is commented out) and it always stays the same color(it isn't one in the array, it's the default color of the prefab).
void Start () { //Set colors for tiles colors[0] = new Color(242, 103, 103); //Pink colors[1] = new Color(80, 206, 196); //Blue colors[2] = new Color(147, 206, 80); //Green colors[3] = new Color(238, 174, 79); //Orange //Create the number of tiles needed GameObject[] tiles = new GameObject[numRows * numColumns]; for (int i = 1; i <= tiles.Length; i++) { tiles[i-1] = Instantiate(tilePrefab) as GameObject; tiles[i-1].transform.SetParent(gameCanvas.transform); tiles[i-1].transform.position = new Vector3 (x, y, 0f); //tiles [i - 1].GetComponent<Renderer> ().material.color = colors [3]; Renderer rend = tiles[i-1].GetComponent<Renderer>(); rend.material.color = colors [3]; x += 320; if (i % 3 == 0) { x = 300; y -= 320; } }
Everything I find online does not work or is either outdated like renderer.material.color
edit: I apologize for the code being like that, it looks organized in the text box as I edit it, but when I save, it bunches it into a paragraph
I just did a google search. first result man. https://docs.unity3d.com/ScriptReference/$$anonymous$$aterial.SetColor.html Hope that helps! :D
Yeah I came across that as well and that doesn't seem to work for me either. I'm not sure what is going on at this point.
If it helps, I am using a blank image 250x250 and changing the background color. I am using that as a prefab for my "tile object". Then in my GameScript I am creating an array of game objects and trying to change the color in a loop as I instantiate them.
Answer by choustonator · Jan 09, 2017 at 06:03 AM
I found my mistake! After further looking online and playing around on Unity, I was using a GameObject for my image, however I needed to change it from GameObject to Image in my code and include "using UnityEngine.UI" and then tiles[i-1].color = Color.red worked.
Sorry for being a noob!
Follow this Question
Related Questions
can you control 2 gameobject color values without having to reference both ? 0 Answers
How to click in screen and change color in my sphere? 2 Answers
OnMouseOver wont work with fps Controller 0 Answers
change color of part of plane 1 Answer
How to change the alpha of every GameObject with the same Tag ? 1 Answer