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;
}
}
}