Object material save in PlayerPrefs

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 in PlayerPrefs. I couldn’t find any posts to help me…Can you help me? Thanks!

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’re not going to store the material itself in PlayerPrefs. Instead store an identifier, and when you read back the identifier you pick the correct material based on it.

From your code it looks like they are mostly different colors, so you could create an enum and convert to int, or you could just write the color as a string. Then when reading back you use either a dictionary or a switch statement to get the correct material set.

1 Like