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.

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!

Wow syclamoth, I would have never thought of something like that :slight_smile: I trying it now and I seriously appreciate the advice brb.

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 :slight_smile:

Hey syclamouth,

I seriously appreciate the help and effort in trying to figure this out :slight_smile:

exiguous from the forums actually solved it and hats off to him :wink: 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!

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