How would I change the color of an object thats going to be instantiated? but not change the color of the ones that are already instantiated?
Im instantiating a block (cube) several times
var cube = Instantiate(block, finalTemp, Quaternion.identity);
cube.name = "Cube";
and Im using a color picker
var xxx : float = 10;
var yyy : float = 10;
var colorPicker : Texture2D;
// assign your model to this variable//commented out this was for testing the picker
//var model : Renderer;
// The material index of the material you want to pick a color for.
var matnum : int = 0;
//Color Picker
function OnGUI()
{
if (GUI.RepeatButton (Rect (xxx,yyy,281,135), colorPicker))
{
var pickpos : Vector2 = Event.current.mousePosition;
var pixelPosX : int = pickpos.x-xxx-9;//9
var pixelPosY : int = 41-(pickpos.y-yyy-5);//5
var col : Color = colorPicker.GetPixel(pixelPosX,pixelPosY);
//model.materials[matnum].SetColor("_Color",col);
}
}
How would I change the color of the Object thats about to get instantiated then pick another color and not affect the ones that are already instantiated and have the new color only affect the next blocks or cubes that im going to instantiate?
Hopefully the question wasn’t too confusing?
I’ve been searching posts and came up with these suggestions but Im exhausted and cant think clearly and I need some help if you could?
//block.renderer.materials[matnum].SetColor("_Color",col);
//GameObject.Find("Cube").renderer.materials[matnum].SetColor("_Color",col);
//var sm = GameObject.Find("Cube");
//sm.renderer.sharedMaterials[matnum].SetColor("_Color",col);
I guess it may not be the best way to do it, and am sure you thought about it but you don’t want this way to do it, but creating a new material maybe ?
But that’s not really efficient if you have a lot of cube/color to instantiate : /
var cube = Instantiate(block, finalTemp, Quaternion.identity);
cube.name = "Cube";
cube.renderer.material.color = new Color(1.0f, 0.0f, 0.0f); // or what you retrieve from picker
does not work?
I’d agree with exiguous, you need to modify the object you instantiate, otherwise you’re changing the prefab, and thus everything you’ve already instantiated.
I appreciated the responses but Im not sure how to write the syntax?
I’ve tried almost 20 different variations and still trying. I think Im getting closer but still no luck, this one here
var sm = GameObject.Find("Cube");
sm.renderer.material.color = col;
Will change the color of the first cube thats instantiated but none of the others, then when I change the color from the picker only the first cube will get the new color and none of the rest?
I think and I could be wrong all of the cubes that are instantiated have the same name and when I change the color from the color picker it only picks the first one and not the ones that will be instantiated next.
Well Im still trying and hopefully I’ll get it soon? If anyone wants to help it would be greatly appreciated!
your code will change the color of one cube. if you need to change it for many cubes and after their creation you need to store a reference to each cube in an array/list and then loop through it and change their color as needed.
right. unity cannot create a name automatically for a gameobject as it doesnt know the purpose you need it for. but you are free to change the name after creation to for example cube_01 so you will have unique names for each and you could also use find this way.
as i told it changes the color of the instance you call the code for. if you have complicated coloring schemes you need to think a way of handling it.
but i have not understood what you want to achieve.
if you just want to change the color of the next created cubes you could also store it and assign it to the cubes you create next.
Color cubecol; // change this with your colorpicker
int cubecnt = 0; // increase this when you instantiate a cube
// call this in function of button/key pressed or whatever
var cube = Instantiate(block, finalTemp, Quaternion.identity);
cube.name = "Cube_"+cubecnt.ToString();
cubecnt++;
cube.renderer.material.color = cubecol;
@ exiguous ahhh… Your a genius myFriend 
I cant thank you enough!!! Greatly appreciated!!! It works perfectly. I’ve struggled with this for the last few days and didn’t think I’d ever forget it, until your post.
var blockPrefab : Transform;//block prefab
var xxx : float = 10;// position on x axis
var yyy : float = 10;// position on y axis
var colorPicker : Texture2D;// assign your colorpicker texture to this variable
var cubecol : Color; // change this with your colorpicker
var cubecnt : int = 0; // increase this when you instantiate a cube
//instantiate code here
if (Input.GetMouseButtonDown(0))
{
var cube = Instantiate(blockPrefab, finalTemp, Quaternion.identity);
cube.name = "Cube1_"+cubecnt.ToString();
cubecnt++;
cube.renderer.material.color = cubecol;
}
function OnGUI()
{
if (GUI.RepeatButton (Rect (xxx,yyy,281,135), colorPicker))
{
var pickpos : Vector2 = Event.current.mousePosition;
var pixelPosX : int = pickpos.x-xxx-9;//9
var pixelPosY : int = 41-(pickpos.y-yyy-5);//5
var col : Color = colorPicker.GetPixel(pixelPosX,pixelPosY);
cubecol = col;
}
}
AGAIN THANK YOU SOOOO VERY MUCH!
I love Unity Forums Unity Answers absolutely amazing people, thanks guys you make it GREAT!
i’m happy that it works now. you are welcome.