- Home /
How to save a material using PlayerPrefs?
I'm using player prefs for my shop system. If i select a skin it gets equipped and then when the player dies the skin reverts back to the default skin. Also, if I restart my game then also skin is not saving and the game loads the old one.
Here is my scripts using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class skinSelection : MonoBehaviour {
public Renderer skinRender;
public Material mat1, mat2, mat3;
public static bool skinsel1 = false, skinsel2 = false, skinsel3 = false, skinsel4 = false, skinsel5 = false;
public void equip1()
{
Shop.isSkinPur1 = true;
if(Shop.isSkinPur1 == true)
{
skinsel1 = true;
skinsel2 = false;
skinsel3 = false;
skinsel4 = false;
skinsel5 = false;
}
}
public void equip2()
{
Shop.isSkinPur2 = true;
if (Shop.isSkinPur2 == true)
{
skinsel2 = true;
skinsel1 = false;
skinsel3 = false;
skinsel4 = false;
skinsel5 = false;
}
}
public void equip3()
{
Shop.isSkinPur3 = true;
if (Shop.isSkinPur3 == true)
{
skinsel3 = true;
skinsel1 = false;
skinsel2 = false;
skinsel4 = false;
skinsel5 = false;
}
}
public void equip4()
{
Shop.isSkinPur4 = true;
if(Shop.isSkinPur4 == true)
{
skinsel4 = true;
skinsel1 = false;
skinsel2 = false;
skinsel3 = false;
skinsel5 = false;
}
}
public void equip5()
{
Shop.isSkinPur5 = true;
if(Shop.isSkinPur5 == true)
{
skinsel5 = true;
skinsel1 = false;
skinsel2 = false;
skinsel3 = false;
skinsel4 = false;
}
}
}
And the one which saves the selection of skin
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class skinMANAGER : MonoBehaviour
{
public Material skin1, skin2, skin3, skin4, skin5;
public Renderer ballRen;
//string saveSkin = "skinEquipped";
public void skinChanged()
{
if(skinSelection.skinsel1 == true)
{
ballRen.sharedMaterial = skin1;
skinSelection.skinsel2 = false;
skinSelection.skinsel3 = false;
skinSelection.skinsel4 = false;
skinSelection.skinsel5 = false;
_saveSKINS();
}
else
{
Debug.Log("Can't Equip");
}
}
public void skinChanged2()
{
if(skinSelection.skinsel2 == true)
{
ballRen.sharedMaterial = skin2;
skinSelection.skinsel1 = false;
skinSelection.skinsel3 = false;
skinSelection.skinsel4 = false;
skinSelection.skinsel5 = false;
_saveSKINS();
}
else
{
Debug.Log("Can't Equip");
}
}
public void skinChanged3()
{
if(skinSelection.skinsel3 == true)
{
ballRen.sharedMaterial = skin3;
skinSelection.skinsel1 = false;
skinSelection.skinsel2 = false;
skinSelection.skinsel4 = false;
skinSelection.skinsel5 = false;
_saveSKINS();
}
else
{
Debug.Log("Can't Equip/;");
}
}
public void skinChanged4()
{
if (skinSelection.skinsel4 == true)
{
ballRen.sharedMaterial = skin4;
skinSelection.skinsel1 = false;
skinSelection.skinsel2 = false;
skinSelection.skinsel3 = false;
skinSelection.skinsel5 = false;
_saveSKINS();
}
else
{
Debug.Log("Can't Equip");
}
}
public void skinChanged5()
{
if(skinSelection.skinsel5 == true)
{
ballRen.sharedMaterial = skin5;
skinSelection.skinsel1 = false;
skinSelection.skinsel2 = false;
skinSelection.skinsel3 = false;
skinSelection.skinsel4 = false;
_saveSKINS();
}
else
{
Debug.Log("Can't Equip");
}
}
public void _saveSKINS()
{
PlayerPrefs.SetInt("skinEquipped", 1);
PlayerPrefs.Save();
}
}
And yes I'm using playerprefs.get int at start to load my skins. Doesn't work.
you could also save it all in scriptable objects and then save to a file.
Answer by logicandchaos · May 20, 2021 at 03:48 PM
You could make materials a list, then make a int for the index and sae the index to playerprefs. And then you could skip all the tedious bool stuff.
public Renderer skinRender;
public List<Material> materials;
public int materialIndex;
//then you can do something like this:
public void equip(int p_index)
{
materialIndex=p_index;
whatever=material[p_index]
}
public void _saveSKINS()
{
PlayerPrefs.SetInt("skinEquipped", materialIndex);
PlayerPrefs.Save();
}
Your answer
Follow this Question
Related Questions
1st Problem: OnLevelWasLoaded, method is getting called twice. 2nd: Loading PlayerPrefs 1 Answer
playerprefs spawning errors. 1 Answer
i need to figure out how to add to a Player pref int 1 Answer
Coins not updating after making shop transactions 0 Answers
Save different game object's datas using the same script? 3 Answers