Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 animeman0 · Sep 12, 2015 at 01:24 PM · c#playerprefsscoreint

Can PlayerPrefs be accessed in different scripts - c#

What I'm trying to do is use PlayerPrefs so I can can keep the score the same when the level is reloaded, but I need to use it in a different script so I can add to the score as well. Is this possible?

Script 1:

 void Awake()
     {
         PlayerPrefs.SetInt("lives", 3);
         PlayerPrefs.SetInt("score", 0);  
     }
 public void ScoreText()
     {
         scoreText.text = "Score: " + PlayerPrefs.GetInt("score");
     }
     public void Wintext()
     {
         if (PlayerPrefs.GetInt("score") == 100)
         {
             winText.text = "You Win!";
         }
     }

script 2:

 public void OnTriggerEnter2D(Collider2D other)
     {
         if(other.CompareTag("Enemy"))
         {
             Destroy(other.gameObject);
             if(!other.isActiveAndEnabled)
             {
                 PlayerControllerScript.+= 10; //Here is where I need to use the PlayerPrefs
                 PlayerControllerScript.ScoreText();
             }
         }
         Destroy (gameObject);
     }



Comment
Add comment · Show 2
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 Rostam24 · Sep 12, 2015 at 03:10 PM 1
Share

Any reason you are not using a static class to hold the score (static classes won't reset when the level is reloaded)?

avatar image maccabbe · Sep 12, 2015 at 03:57 PM 0
Share

http://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by pako · Sep 12, 2015 at 06:47 PM

Yes you can access PlayerPrefs from different scripts, but PlayerPrefs is really meant for storing values between sessions. That's its main use.

If you just want to store score between levels it seems best to use a static class as @Rostam24 pointed out.

However, it's a bit unclear what you really want to do:

 PlayerControllerScript.+= 10; //Here is where I need to use the PlayerPrefs

You must have missed something there. Doing some guesswork, it seems that Script 1 is your PlayerControllerScript and you want to access it from Script 2, because there's nothing in your code showing two different scripts accessing PlayerPrefs. You only show Script 1 accessing PlayerPrefs.

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 animeman0 · Sep 13, 2015 at 10:32 AM 0
Share

Yeah I didn't know what a static class was but I watched the tutorial and I'm implementing it into the code.

avatar image
1

Answer by Shoon · Sep 12, 2015 at 07:10 PM

Of course you can do that, PlayerPrefs can be accessed from anywhere.

You would just do this to increse by 10 your PlayerPref:

 PlayerPrefs.SetInt("score", PlayerPrefs.GetInt("score") + 10);

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

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

30 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

Related Questions

Playerpref Saving Lag 1 Answer

PlayerScore prefab in unity gives value in gameover screen as 0, how do I show the final value in GameOver scene? 0 Answers

Score value in GameOver screen is 0 instead of final value from the Game Level scene(used PlayerPrefs), how do I display the final value from Game Level scene in the GameOver scene? 1 Answer

save a int with playerprefs for one instance of a gameobject 0 Answers

Highscore and PlayerPrefs unity C# 1 Answer


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