Change color of 48 cubes in one script

I am working on a puzzle game and need to assign colors to all the objects. It works similar to Tetris Attack where blocks stack up and you use a paddle to move them.

I have all the cubes setup now and am trying to figure out a way to access them all from one script. I have been lurking and reading for a few months now but this seemingly basic topic has eluded me. Its the whole tags, GameObject.Get… stuff that I don’t understand what I need to use where. When reading tutorials this step is usually skipped over pretty quickly and without detail.

Right now I am trying not to put a script on each object and then tie that into another script. I would like just one to do all the work.

I just need to be able to assign basic colors to the objects for now something like (renderer.material.color = Color.red;)

Can anyone point me in the right direction?

assige the tags to the boxes, and from script you can access them with GameObject.FindGameObjectsWithTag (“tag name”); Here is the example:

var mat : GameObject[];
function Update ()
{
	mat = GameObject.FindGameObjectsWithTag ("block");
	
	for (i = 0; i < mat.length; i ++)
	{
		mat[i].renderer.material.color = Color.red;
	}
	
}

I created a variable that will store all gameObjects with a tag “block” in a array, then I made a for loop that will loop the objects and assigne them a red color.
I hope this helps you.

That is really not a good way to do it. If you need one material, use one material, not 48 of them. Just alter the sharedMaterial once. I keep a reference to the sharedMaterial in a variable somewhere.

I don’t know Tetris Attack, though. If you’re going to need 48 materials again, when something else changes, then that’s not a good approach. But I bet that all you need is a handful of materials, not 48 distinct ones. Always use as few materials as possible, for performance and ease of use.

That works pretty good. I finally understand the ideas behind what I was doing wrong. Thank you!

This did plug right in and work. I was able to fiddle with it and solve some other problems also. Now that I know enough about how it works I need to do it a little differently. It seems unity randomly picks the order the blocks are listed in the array for the tags. This is fine if I am trying to make them all one color but to assign individual values also I think I will have to make 48 different tags and alter this script a little bit.

Again thank you! I giggled when it worked.

Instead of making 48 separate tags I am finding that the only easy way to do with this and still deal with the fact that it does not keep the blocks in any certain order is to try and find the current target of each instance of the ‘blocks’ tag each time the game is run. Throw that information into an array so I can find them no matter what it decides to do on its own.

Other issues aside, the suggestion is not to make 48 separate tags, but rather to assign the same tag to all ‘block’ game objects so that you can find them easily using FindGameObjectsWithTag().