Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by andrer11444 · Nov 13, 2020 at 04:22 AM · playerprefssavelevelsave datalevels

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrer11444 · Nov 13, 2020 at 11:48 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

223 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Are playerprefs a safe way of saving data or not?? 2 Answers

Saving last runs score, only adds the previous score to it. 0 Answers

How can I save values in an array using PlayerPrefs? 0 Answers

Saving In Between Scenes and Program Exit 0 Answers

Bug with Lock Unlock System 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges