Hi, i’m a new developer and i am making a mobile game.
I barely understand c# so could someone help me because i can’t find any solution for my problem
i don’t understand why isn’t this script working
console give me an error on this line: PlayerPrefs.SetString (“CurrentSprite”, sr.sprite);
my script:
using UnityEngine;
using System.Collections;
public class playerspriteimagechange : MonoBehaviour {
public GameObject ball;
public Sprite pinksquare;
public Sprite triangle;
public Sprite ball2;
private SpriteRenderer sr;
void Start()
{
sr = GetComponent<SpriteRenderer> ();
}
public void onClick()
{
sr.sprite = triangle;
PlayerPrefs.SetString ("CurrentSprite", sr.sprite);
}
You can’t use PlayerPrefs to save a component like a SpriteRenderer, plain and simple. If you raly want to use PlayerPrefs for this, then you have to find a workaround. For example, you could store the name of the GameObject the SR is on, and then load that name again and use it with GameObject.Find to get teh GameObject in the scene, and then use GetComponent to access the SpriteRenderer.
Edit: If you are getting an error, please post the actual error message.
Replying Late but saw the question was not marked with an accepted answer
You can only store String in PlayerPrefs if you use “PlayerPrefs.SetString (“CurrentSprite”, sr.sprite);”
What you can do is store all sprites that you might be changing through in an array and store the Int value of accessing it. When your game begins, re-assign the correct sprites using PlayerPrefs in awake so the player will not see old sprites.
In case you needed a workaround and used any other way, let me know. I personally use this idea most of the time.