Cloning a component in runtime!

Hi all,

Im stuck and tried to find the solution for this problem everywhere but with no luck what im trying to do conceptually is the following.

Player select a building, the building following the cursor , when the spot is not fit for building the building turns red and when its fit again the building get back to its original state.

what im doing programmatically is that im going throught the selected building all childs and turning their materials in their

Renderer[] _renderer = transform.GetComponentsInChildren<Renderer> ();
			for (int i=0; i<_renderer.Length; i++) {

				origRenderer[i]=renderer[i];
				for (int j=0; j<_renderer[i].materials.Length; j++) {
					Color temp = Color.red;
					temp.a = 0.45f;
					_renderer [i].materials [j].shader = Shader.Find ("Transparent/Diffuse");
					_renderer [i].materials [j].color = temp;
				}
}

The problem now is that i tried saving the renderers into an array but they seem to be pointers which turns also red and i can’t go back to the original state.
is there a solution for this or i have to take another approach im out of ideas for now.

Thanks in advance

Careless,

For me, personally, I find adjusting materials and shaders via scripting to be messy. I would probably have two different pre-fabs, one for the regular building, one for the red building, and instantiate them both when trying to build. Then I would just switch which one is visible (either with renderer.enabled or GameObject.SetActive), depending on conditions at the build site.

Cheers!
Cahman

I did something like this recently with triggers. My buildings were controlled by arrow keys instead of cursor but the concept would work the same. The project I created was on a grid system like most RTS games so I created a group of boxes that would cover every possible building layout. Before I decided to go strictly with triggers I had the whole system working with raycast that followed my buildings under the surface (pointed up). If they hit anything material color changed to red, and if not to green.

Hope this gave you some ideas. Check it out here ( early proto )

Thank you guys,
The 2 gameobject and swapping is basically what i thought of (Cahman hint) for now im creating one original gameobject that is not in the camera view and whenever the building turns red its fine and when im turning back to orig im getting the gameobject(the one not in camera sight) material and applying it to the one on mouse hold, doesn’t sound ideal but its working for now.
Black Mantis, Thank you for the idea might use it soon