PlayerPref doesnt work
Hello
I am just working with the Book from Adam Sinicki. Right now i am stuck, cus its not clearly explained. I have 2 Levels and one Level select scene. In the level select Scene i have two images and an selector frame. In the first actual Level i have an object at the end to load the next level when my player collides with it. This Object carries the LevelValue(2). This will be saved in PlayerprefSetInt("farthestLevel", value). Now in the select Scene, the image of Level 2 is covered by an other picture, cus it shouldnt be playable before you havent finished level 1. But somehow it doesnt work. i play the first level, then the playerpref saves the value 2. But the second Image of Level 2 doesnt change from this.tag="off", to "on". But i can see at lest it take the tag "off". And farthestLevel=PlayerPref didnt work. Here is the code
NextLevel: public class NextLevel : MonoBehaviour {
public string nextLevel;
public int LevelValue;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
SaveLevel(LevelValue);
SceneManager.LoadScene(0);
}
}
public void SaveLevel(int Level)
{
PlayerPrefs.SetInt("farthestLevel", Level);
}
}
Code of the selector
public class Selector : MonoBehaviour {
public bool moveLeft;
public bool moveRight;
private string LevelChoice;
public int farthestLevel;
//bewegung selector verbessern
private int bewegen = 12;
//private int rechtsbewegen = -12;
// Start is called before the first frame update
void Start()
{
PlayerPrefs.GetInt("farthestLevel",farthestLevel);
}
// Update is called once per frame
void Update()
{
if(transform.position.x > -5 && (moveLeft || Input.GetKeyDown(KeyCode.LeftArrow)))
{
transform.position = new Vector2(transform.position.x - bewegen , transform.position.y);
}
else if (transform.position.x < 7 && (moveRight || Input.GetKeyDown(KeyCode.RightArrow)))
{
transform.position = new Vector2(transform.position.x + bewegen, transform.position.y);
}
if (Input.GetKey(KeyCode.Space))
{
Select();
}
}
public void Select()
{
SceneManager.LoadScene(LevelChoice);
}
public void OnTriggerEnter2D(Collider2D collision)
{
LevelChoice = collision.name;
}
}
And the code of the LevelLoade, what is attached to the second Level Image.
public class LevelLoader : MonoBehaviour { public int thisLevel; private Selector sl;
// Start is called before the first frame update
void Start()
{
sl = FindObjectOfType<Selector>();
}
// Update is called once per frame
void Update()
{
if(sl.farthestLevel < thisLevel)
{
this.tag = "off";
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<BoxCollider2D>().enabled = false;
}
else
{
this.tag = "on";
GetComponent<SpriteRenderer>().enabled = true;
GetComponent<BoxCollider2D>().enabled = true;
}
}
}
Answer by andrer11444 · Nov 13, 2020 at 05:21 AM
I also can see that the Value "farthestLevel" of the Selector, doesnt change to 2 after finishing Level 1, i saw it in the Inspector, but why? As soon i finish the Level, the Value must go somewhere, somehow playerpref maby doesnt save it right?
Answer by LordDAVidus · Nov 13, 2020 at 11:35 AM
Change the code In Start of Selector to:
farthestLevel = PlayerPrefs.GetInt("farthestLevel");
To set and get a PlayerPref you need to do it like this:
int intinScene1; //the Int you want to store
int intinScene2; //the Int you want to set
PlayerPrefs.SetInt("MyInt", intinScene1); //Save the Int in Scene1
intinScene2 = PlayerPrefs.GetInt("MyInt"); //Set the Int in Scene2
Hello, thanks for your help, the first one i already tried and it dindt work, ill try the second approach and change it again in the selector,then ill come back here if it still doesnt work.