Way to not change prefab on changing instantiated prefab's value?

Hello,

I’m using following code to Access some Projectors and change their materials color, but I don’t want to save that into my existing prefabs.

// Get all projectors for manipulating em
				Projector proj = GetComponentInChildren<Projector>();
				int projIndex = 0;

				Color c1 = new Color(1, 0, 0);
				proj.material.color = c1;

Do you know a way to get them not to save into the prefabs?

Cheers

Where do you actually execute that snippet of code? Is it in a MonoBehaviour script?

Your problem is probably that you have assigned a Material asset to the material property of the projector. Changing the color property of the material asset will of course be saved. You either need to instantiate / copy the material, or use different materials.

To instantiate the material you can use:

    Material m = (Material)Instantiate(proj.material);
    proj.material = m;
    m.color = new Color(1, 0, 0);

Renderer components have two different material properties: material and sharedMaterial. If you access material it will automatically create an instance of the material if it doesn’t have one yet. The Projector however only has the direct reference to the Material which is stored in the sharedMaterial on a Renderer. Changing something on the sharedMaterial will also modify the linked Material asset.

If you generally need a seperate material for each projector you should attach a script to the projector with this start method:

void Start()
{
    var proj = GetComponent<Projector>();
    proj.material = (Material)Instantiate(proj.material);
}

This will create a personal instance for thie projector. Now when using your original code it won’t affect the Material asset since you work on a copy.