- Home /
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
I believe the issue you're running into is changing the base object of the prefab. They get instantiated with the values associated with the prefab in your project. If you want to change anything specific to the player while they share the same prefab, you would need to create a reference to the instance and not the prefab.
e.g. I make 2 balls. They are both following a base prefab of ball. Ball has an initial color of green, so both balls will be green when they are created because they are referencing the object to be cloned from. To change the color, you need a reference to the instance of each. You can do that by instantiating the objects and casting them as a GameObject and store them in their own variable. This will allow you to access the color of each specific ball made.
I gave my 2 cents to potentially clarify any issues you had with understanding the answers provided. @Hanoble pretty much answers it for you.
I did that, and it seems to work fine, but the issue I'm having is I don't know how to actually change the color of a specific material (given that there are multiple different materials applied to the same mesh) on one of the instances via code.
This may help you. SetColor I believe you can target the material you want. Just make proper references and you should be fine.
Answer by JedBeryll · Sep 06, 2016 at 05:49 PM
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.
Thanks for the quick reply!
I'm not really sure how to do that though, would you $$anonymous$$d walking me through it a bit?
Answer by Hanoble · Sep 06, 2016 at 06:58 PM
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.
That seems to work well for instantiation, but I'm still not really sure how to change the individual material colors. Here's what I have so far:
public GameObject player;
public player_scr script1;
public player_scr script2;
// Use this for initialization
void Start () {
GameObject Player1 = (GameObject) Instantiate (player, new Vector3 (5, 10, 0), Quaternion.identity);
GameObject Player2 = (GameObject) Instantiate (player, new Vector3 (-5, 10, 0), Quaternion.identity);
script1 = Player1.GetComponent<player_scr> ();
script2 = Player2.GetComponent<player_scr> ();
script1.bodycolor.SetColor ("_Color", Color.green);
script2.bodycolor.SetColor ("_Color", Color.blue);
script1.playernumber = 1;
script2.playernumber = 2;
}
Changing the "playernumber" variable works, but changing the "bodycolor" variable (which contains the material I want to target) still changes both. Are there other methods for changing the material that I could try?