Search Unity

losing numbers when transfer to another scene ?

Discussion in 'Scripting' started by Quast, Nov 24, 2015.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I don't know why. but it become a headache
    where is the problem ? i need to keep all numbers save and not return to default (ZERO)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5.  
    6. public class ballpower : MonoBehaviour {
    7.    
    8.      public int levelsnumbers;
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.            
    13.         levelsnumbers = 0;
    14.  
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         level1.enabled = false;
    20.  
    21.    }
    22.  
    23. void OnTriggerEnter (Collider other)
    24.     {
    25.          
    26.         if (other.gameObject.CompareTag("eatj"))
    27.         {
    28.             other.gameObject.SetActive(false);
    29.             levelsnumbers =  levelsnumbers+ 50;
    30.             setcount();
    31. }
    32. }
    33.  
    34.  
    35.     void savedataformap()
    36.     {
    37.         PlayerPrefs.SetInt("levelofmaps", levelsnumbers);
    38.  
    39.     }
    40.  
    41.     void loaddataformap()
    42.     {
    43.         levelsnumbers = PlayerPrefs.GetInt("levelofmaps");
    44.     }
    45.  
    46. if (levelsnumbers >= 50)
    47.         {
    48.             level1.enabled = true;
    49.            // level1.image.color = Color.green;
    50.         }
    51.  
    52.     /////////
    53. }
    54.  
    55.  
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    You're never calling loaddataformap in the code you've pasted.

    The code you have pasted also won't compile, so I'm guessing something's missing?

    If you want the data written to playerprefs to save between play sessions, you have to do a PlayerPrefs.Save() at one point.
     
    BenZed likes this.
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    what I'm missing ?
    Baste, the save and load code it should run auto. So if I go to new scene and use the same code file that have all what i save it, have to be there ?
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    How does it auto run? I would like to know how this magic works. I know that PlayerPrefs.Save() is automatically called when you exit the game(not if it crashes or exits prematurely) and it loads PlayerPrefs at the start of the game, but you have to call the functions to get the data it has.

    As @Baste pointed out savedataformap()/loaddataformap() are never called and Unity will never call them automatically you need to.
     
  5. S_Ven

    S_Ven

    Joined:
    Nov 25, 2015
    Posts:
    1
    if only change the scene,you just need change the “levelsnumbers” to static