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 /
avatar image
0
Question by Reefer · Feb 12, 2017 at 12:25 PM · c#unity5playerprefsscores

Trying to save and load scores from PlayerPrefs

Hello. I'm trying to save and load total points player has got from PlayersPrefs, it works fine so far until next level starts. Total points stays at 0, even though it should show 4000 points after first level if you dont fail anything.

Heres the script thats supposed to load score from PlayerPrefs:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class BoxCol : MonoBehaviour
 {
     public GameObject[] boxes;
     public int Points;
     public static int totalPoints;
 
     void Start()
     {
        totalPoints = PlayerPrefs.GetInt("TotalPoints");
     }
 
     void Update()
     {
         boxes = GameObject.FindGameObjectsWithTag("Box");
         Debug.Log("Boxes: " + boxes.Length);
         Points = boxes.Length * 1000;
 
         if (Points == 0)
         {
             restartCurrentScene();
         }
     }
 
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "Box")
         {
             Debug.Log("Box hit the ground");
             Destroy(col.gameObject);
         }
     }
 
     void OnGUI()
     {
         GUI.Box(new Rect(Screen.width / 2, 30, 200, 20), "Points: " + Points);
         GUI.Box(new Rect(Screen.width / 2, 60, 200, 20), "Total Points: " + totalPoints);
     }
 
     public void restartCurrentScene()
     {
         Scene scene = SceneManager.GetActiveScene();
         SceneManager.LoadScene(scene.name);
     }
 }

And heres the script thats supposed to save that total points score to PlayerPrefs, which it is supposed to get from that script I posted first.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class Finish : MonoBehaviour {
 
     public BoxCol totalPoints;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Truck")
         {
             Debug.Log("Finish line");
             saveScores();
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
         }
     }
 
     public void saveScores()
     {
         PlayerPrefs.SetInt("TotalPoints", BoxCol.totalPoints);
     }
 }
 



I think it has something to do with loading it from another script as I can't put my "BoxCol.cs" script to the place on Inspector that says "BoxCol".

So any ideas how to fix it?

Comment
Add comment · Show 7
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 Bonfire-Boy · Feb 12, 2017 at 03:27 PM 0
Share

I think it has something to do with loading it from another script as I can't put my "BoxCol.cs" script to the place on Inspector that says "BoxCol".

Not sure what that means. If you're trying to assign a GameObject to the totalPoints field of a Finish component in the inspector, and it's not letting you, then you probably don't have a BoxCol component on the GameObject.

But I don't think that should matter because you're not using that field anywhere.

I can't actually see anywhere in the code you've shown, where you're modifying the value of BoxCol.totalPoints. You load it from PlayerPrefs and save it to PlayerPrefs, and that's all. So why are you expecting it to change?

avatar image Reefer · Feb 12, 2017 at 03:39 PM 0
Share

Yes I noticed that itself too and added there a part:

 void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Truck")
         {
             BoxCol.Points = BoxCol.totalPoints;
             Debug.Log("Finish line");
             saveScores();
             Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
         }
     }


But still no luck.

avatar image Bonfire-Boy Reefer · Feb 12, 2017 at 03:44 PM 0
Share

That's still not changing the value of BoxCol.totalPoints. And I wouldn't expect it to compile because BoxCol.Points is a member field and you're trying to access it as if it's static (like totalPoints is).

avatar image Reefer · Feb 12, 2017 at 03:53 PM 0
Share

Sry if I'm being stupid, but doesnt BoxCol.Points = BoxCol.totalPoints tell it that current points are totalPoints when gameobject with tag: Truck hits the trigger?

avatar image Bonfire-Boy Reefer · Feb 12, 2017 at 04:05 PM 0
Share

The point is that that line changes the value of BoxCol.Points, not the value of BoxCol.totalPoints. And BoxCol.totalPoints is what's used in all your PlayerPrefs Set and Get calls. So that's what you need to change (before you call PlayerPrefs.SetInt using it) if you want to change what's stored in PlayerPrefs.

avatar image Reefer Bonfire-Boy · Feb 12, 2017 at 05:20 PM 0
Share

Well even if I try BoxCol.totalPoints = BoxCol.Points its still no luck.

avatar image RobAnthem · Feb 12, 2017 at 05:38 PM 0
Share

After reviewing your code more, my first reply may of been hasty for a couple of reasons. First of all, are heading down a very very bad road. If you get comfortable writing code like this, it will be nothing but bad for you. Do your best to stop "Finding" objects, or $$anonymous$$imize it to OnEnables/$$anonymous$$ethods. There is NOTHING good about Finding objects by Tag or by Name. Despite popular belief. Also, using PlayerPrefs for saving data is NOT suggested, but doable, and at best it should only be done for small amounts of data, and done the proper way.

GameObjects can't be serialized, as such, no data of events should be stored inside a mono script, ins$$anonymous$$d passed to a scriptable object, or a static reference class. Even though playerPrefs is an outside source, you are still treating it like a local thing.

Reloading your scene as a way to restart is a decent method of doing so, but you should utilize the OnEnable/OnDisable methods that you have access to. Honestly the Start method is nearly useless.

You should also look into the 4.6 UI, as it makes OnGUI completely obsolete, and can still be instantiated during runtime, more effectively I might add, with prefabs.

Anyway, I hope some of this helped.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by OmerDoron · Feb 12, 2017 at 08:28 PM

add totalPoints = PlayerPrefs.GetInt("TotalPoints"); on start at 2nd script

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 ayrrik · May 22, 2017 at 08:34 PM

@reefer If you are still having trouble with this, I can help - there's a few things I see wrong here, but don't want to get into it too far if you have already fixed your issue by now...

One big issue I see is where you added "BoxCol.Points = BoxCol.totalPoints;" (found in comments from first post).. you are referring to the BoxCol static value, but you also have a local instance of a BoxCol, which you named "totalPoints". So this line should be more like totalPoints.Points = totalPoints.totalPoints.. so your naming convention is a little funky or you need to remove the instance of BoxCol, and just make Point static as well.

Another issue is I don't see where you are adding or removing points for anything.. Maybe you are doing it in another script or maybe it should be in the "OnCollisionEnter" of BoxCol.. something like: totalPoints -= 1000, or Points -= 1000... or += instead of -=, depending on what you are trying to do here.

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

291 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I want to save scores and keep incrementing from saved score on restarting the game or in next level/scene? 1 Answer

Save/Load Animation State of Instantiated Prefabs 0 Answers

Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 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