Hello! I'm somehow new in game developer, and I have a problem... I have a script that changes the material of an object,the script works but I want the last material that is on the object to save when I close the game, or when I change the scene. I couldn't find any posts to help me...Can you help me? Thanks! Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class ChangePlayerColor : MonoBehaviour { public Material[] material; Renderer rend; private void Start() { rend = GetComponent<Renderer>(); rend.enabled = true; rend.sharedMaterial = material[0]; } public void Update() { } public void Blue() { rend.sharedMaterial = material[1]; } public void Yellow() { rend.sharedMaterial = material[2]; } public void Green() { rend.sharedMaterial = material[3]; } public void Purple() { rend.sharedMaterial = material[4]; } public void Roz() { rend.sharedMaterial = material[5]; } public void Aqua() { rend.sharedMaterial = material[6]; } public void Red() { rend.sharedMaterial = material[0]; } public void FadeBlue() { rend.sharedMaterial = material[7]; }
You will need to use PlayerPref to save the values before the game closes. And reload them again when the game is launched again. https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Adding to Vish's post above, you can't save the actual material, but you would save the index of it. Here's an example of simple persistent loading/saving values using PlayerPrefs: https://pastebin.com/icJSq5zC Useful for a relatively small number of simple values.
Same answer from me as when you asked the same question yesterday https://forum.unity.com/threads/object-material-save-in-playerprefs.988337/