- Home /
Question by
endermanrapman · Jul 31, 2020 at 05:25 PM ·
c#customization
How to change prefab material C#?
So i'm making a game with customization, and the player is a prefab that contains a sphere that represents the player. I can't find out how to change the material of the prefab in each scene, because when I change the color then go to another scene, the material resets.
using UnityEngine; public class Customization : MonoBehaviour {
// Variables
public Material[] material;
public int x;
public Renderer rend;
public GameObject _playerPrefab;
// Start is called before the first frame update
void Start () {
x=0;
rend.enabled =true;
rend.sharedMaterial = material[x];
}
// Update is called every frame
void Update () {
rend.sharedMaterial = material[x];
}
// Switches the material of the prefab
public void NextColor() {
if(x<7)
{
x++;
}
else
{
x=0;
}
}
// Goes back
public void Back() {
if(x>0)
{
x--;
}
else
{
x=8;
}
}
}
Comment
Answer by Llama_w_2Ls · Aug 01, 2020 at 08:50 AM
You could have a script that is set to DontDestroyOnLoad
which basically stores the player's color throughout all the scenes.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004554im_/https://answers.unity.com/themes/thub/images/avi.jpg)