- Home /
Color Picker / Instantiated Clones
How would I change the color of the Object thats about to get instantiated, instantiate a few cubes, 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?
Im instantiating a block (cube) several times:
var cube = Instantiate(block, finalTemp, Quaternion.identity);
cube.name = "Cube";
Im using this 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);
}
}
I've tried so many different variations but cant figure out the correct syntax... This was suggested as well:
cube.renderer.material.color = new Color(1.0f, 0.0f, 0.0f);
but figuring out the right syntax has been a tough one I tried:
var sm = GameObject.Find("Cube");
sm.renderer.material.color = col;
but only the first cube thats instantiated will change color its not working quite correctly. Any suggestions or help would be GREATLY APPRECIATED!!!!
ok getting back to it.
[code] [/code] tags don't work on UA. I've updated your post with proper formatting, but learn how to do it correctly if you're going to post code here. The preview below your post is very helpful for this!
Basically you select all your code once it's pasted in, and press the '101010' button.
I've just updated it :) any chance you can help with he question?
Answer by syclamoth · Nov 03, 2011 at 03:40 AM
The way I would do this, would be to at the beginning of the scene instantiate a 'template' block which is either on an invisible layer, or otherwise outside of the player's view.
Then I would use the colour picker to change the material of that template, and instantiate it instead of the prefab! This way, you can change the colour of new blocks, but 1: you don't change the colour of old ones, and 2: you don't change the colour of the prefab either.
You could even put the 'template' in the corner of the screen as a 'preview' of what the blocks are going to look like!
Another thing- using GameObject.Find(string) is a pretty flaky way of finding things. Try to keep track of your objects in an array, or a list if you want to access them later. (using my method, you don't really need to do this, btw)
Answer by michael777 · Nov 03, 2011 at 03:49 AM
Wow syclamoth, I would have never thought of something like that :) I trying it now and I seriously appreciate the advice brb.
Answer by michael777 · Nov 03, 2011 at 04:31 AM
I tried it but it still seems to be changing the color of all of the blocks.
I put a cube from the hierarchy into the model var slot then did
var cube = Instantiate(model, finalTemp, Quaternion.identity);
and added new cubes which had the correct color but when I chose a new color for the next blocks they all changed to the new color I was now choosing.
I also tried instantiating a cube in the start function then instantiating the new blocks from the temp that was instantiated and I got the same results...
phew... I didn't think it would be this tough
any other ideas? I'll take any :)
$$anonymous$$ake sure you're modifiying 'material', not 'shared$$anonymous$$aterial'.
Wait, is the colour picker on each individual cube, or just on the template cube? $$anonymous$$ake sure that 1: there's only one colour picker, 2: it's definitely modifying the template, and nothing else, and 3: that the instantiated cubes aren't being modified by anything.
Instantiating the template in Start is the correct thing to do, btw.
Answer by michael777 · Nov 04, 2011 at 12:46 AM
Hey syclamouth,
I seriously appreciate the help and effort in trying to figure this out :)
exiguous from the forums actually solved it and hats off to him ;) Here's the solution just incase anyone else needs it.
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;
}
}
Amazing!
I love UnityAnswers & Unity Forums absolutely amazing people, thanks guys you make it GREAT!
Answer by tassawnt · Dec 12, 2020 at 11:42 AM
you can change the color by spriteRenderer color; just create a list of gameoject ,after you instantiate a gameboject add it to a list , then you can change the color of all clones of object by using foreach ,if you don't understant this just say it , and i will explain it more . @michael777
Your answer
![](https://koobas.hobune.stream/wayback/20220613031414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I get the x position for the left(and right) of the screen? 2 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer
Assigning gameobject to an instantiated prefab through script? 2 Answers
How to Instantiate and change velocity 2 Answers
How could I save all cloned objects in a scene, and load them back up on start? 1 Answer