change material of one prefab

Hi,

I’m working on a split-screen game where each player is a prefab of the player object. The player prefab has several materials applied to the same object, and I want to change only one through script.

Currently in-script I have a public material which I have dragged the target material (called goo) into from the inspector. The code I have changes the material like I wanted, but it changes it for every instance, and I only want it to change it for the objects that call the code. Also side note, my current code changes the color permanently, even when I reopen the program.

Thanks for your help!

H. Chappee

Then you are changing the prefab value. When you load your prefab (maybe with Resources.Load) you are assigning the material to that object which leads to the behaviour you described. Instantiate it and save the instance in a variable then change the material of the instance.

When you instantiate the object, you need to do this to get that specific instance.

 GameObject instanceOfPlayer = Instantiate(playerPrefab, spawn.position, Quaternion.identity);

Now you have an instanceOfPlayer object, which is the actual instantiated object, not the prefab. Once you have that you can change the material of that object, by getting the renderer of instanceOfPlayer and changing it there.