- Home /
Completely Black Material - Player Prefs
Hey there! So recently I've been trying to throw together a car customization script, but I've run into a road block. I tried to save the material using an playerpref int, checking the int's value and setting the materialid which changes the material, but when I try to restart the scene after saving the material is completely black. Any ideas on what I did wrong?
Customization Code:
using UnityEngine;
using System.Collections;
public class Customize : MonoBehaviour {
public Material[] materials;
[HideInInspector]
public Material currentMaterial;
[HideInInspector]
public int currentMaterialId;
public GameObject car;
[HideInInspector]
public GameObject chasis;
// Use this for initialization
void Awake () {
chasis = GameObject.FindGameObjectWithTag("Chasis");
if (PlayerPrefs.GetInt("material") == 1)
{
currentMaterialId = 1;
}
if (PlayerPrefs.GetInt("material") == 2)
{
currentMaterialId = 2;
}
if (PlayerPrefs.GetInt("material") == 2)
{
currentMaterialId = 3;
}
}
// Update is called once per frame
void Update () {
if (currentMaterialId == 1)
{
//.<Renderer>().material = materials[1];
chasis.GetComponent<Renderer>().material = materials[0];
PlayerPrefs.SetInt("material", 1);
}
if (currentMaterialId == 2)
{
chasis.GetComponent<Renderer>().material = materials[1];
PlayerPrefs.SetInt("material", 2);
}
if (currentMaterialId == 3)
{
chasis.GetComponent<Renderer>().material = materials[2];
PlayerPrefs.SetInt("material", 3);
}
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 100, 50), "Material 1"))
{
currentMaterialId = 1;
}
if (GUI.Button(new Rect(10, 70, 100, 50), "Material 2"))
{
currentMaterialId = 2;
}
if (GUI.Button(new Rect(10, 130, 100, 50), "Material 3"))
{
currentMaterialId = 3;
}
if (GUI.Button(new Rect(Screen.width/2, Screen.height/100, 100, 50), "Save"))
{
PlayerPrefs.Save();
}
}
}
Answer by Louis-LCM · May 10, 2016 at 11:25 AM
@gosaints70 The problem is with your playerprefs. Try this
using UnityEngine; using System.Collections;
public class Customize : MonoBehaviour {
public Material[] materials;
[HideInInspector]
public Material currentMaterial;
[HideInInspector]
public int currentMaterialId;
public GameObject car;
[HideInInspector]
public GameObject chasis;
// Use this for initialization
void Awake () {
chasis = GameObject.FindGameObjectWithTag("Chasis");
currentMaterialId = PlayerPrefs.GetInt("CurrentMaterial");
}
// Update is called once per frame
void Update () {
if (currentMaterialId == 1)
{
//.<Renderer>().material = materials[1];
chasis.GetComponent<Renderer>().material = materials[0];
PlayerPrefs.SetInt("CurrentMaterial", currentMaterialId);
}
if (currentMaterialId == 2)
{
chasis.GetComponent<Renderer>().material = materials[1];
PlayerPrefs.SetInt("CurrentMaterial", currentMaterialId);
}
if (currentMaterialId == 3)
{
chasis.GetComponent<Renderer>().material = materials[2];
PlayerPrefs.SetInt("CurrentMaterial", currentMaterialId);
}
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 100, 50), "Material 1"))
{
currentMaterialId = 1;
}
if (GUI.Button(new Rect(10, 70, 100, 50), "Material 2"))
{
currentMaterialId = 2;
}
if (GUI.Button(new Rect(10, 130, 100, 50), "Material 3"))
{
currentMaterialId = 3;
}
if (GUI.Button(new Rect(Screen.width/2, Screen.height/100, 100, 50), "Save"))
{
PlayerPrefs.Save();
}
}
}
Jeez, this is an old question but regardless thanks for taking the time to answer! I quickly reread through my code and realized how bad I used to be. Thanks again!
@gosaints70 No problem. Please mark the answer as correct.