Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

PlayerPrefs

Discussion in 'Scripting' started by ks9109, Apr 10, 2014.

  1. ks9109

    ks9109

    Joined:
    Jun 25, 2013
    Posts:
    56
    Hi needed Help Attempting to send score int to next scene i know i need to use player prefs like mike did in the tutorial but how would i use it in JavaScript because i have the workings of it complete but.... Dont know where to go from here Any help please Script is below


    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5. static var coin : int = 0;
    6. private var hit : RaycastHit;
    7.  
    8.  
    9. function Start () {
    10.  
    11.      
    12.  
    13. }
    14.  
    15. function Update () {
    16.  
    17.     //touch input control
    18.  
    19.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    20.     if (Input.GetMouseButtonDown(0))
    21.     {
    22.         if (Physics.Raycast (ray, hit , Mathf.Infinity))
    23.         {
    24.        
    25.             if (hit.collider.tag == "breakable")
    26.            
    27.             {
    28.    
    29.                 audio.Play();
    30.                 coin+=1;
    31.            
    32.         }
    33.        
    34.    
    35.    
    36.         }
    37.         }
    38.         }
    39.  
     
  2. iMob

    iMob

    Joined:
    Apr 9, 2014
    Posts:
    55
    Hello,
    if you want to save the coin int to playerPrefs insert following line at line 29 in your code above:

    Code (csharp):
    1.  
    2. PlayerPrefs.SetInt("Coin", coin);
    3.  
    (Where "Coin" is the Name of the Field, where the int (coin) will be saved.)

    To load the int value in the next scene, you can add following line in Start() of a gameObject of the next scene:

    Code (csharp):
    1.  
    2.  coin = PlayerPrefs.GetInt("Coin");
    3.  
    But you can do another way, without playerPrefs, if you not really need to store the "coin" for use in another game-session.

    You can make a gameObject named "Coin" and add following script to it:
    Code (csharp):
    1.  
    2.  
    3. static var coin : int = 0;
    4.  
    5. function Awake () {
    6.  
    7. // prevent this gameObject (and it´s children) to be destroyed after loading another scene.
    8. // This gameObject will be there, if the new scene is loaded and you can use values from previous scenes.
    9.  
    10.     DontDestroyOnLoad (transform.gameObject);
    11. }
    12. function Update () {
    13.   // your code....
    14. }
    15.  
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    You're already using a static variable - that will do what you want, to keep the value of "coin" through to the next scene. (Though not between sessions) If that does not already work, then something else is going wrong.
     
  4. Entwicklerpages

    Entwicklerpages

    Joined:
    Aug 31, 2013
    Posts:
    14
    Also, if you want to save the PlayerPrefs to save the coins permamently you must call PlayerPrefs.Save() to save them to disk.
     
  5. ks9109

    ks9109

    Joined:
    Jun 25, 2013
    Posts:
    56
    okay i took your advice but have no errors but still there is a problem nothing will show as far as the string of the score is concerened any help.

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5. static var coin : int = 0;
    6. private var hit : RaycastHit;
    7.  
    8.  
    9. function Start () {
    10.  
    11.      
    12.  
    13. }
    14.  
    15. function Update () {
    16.  
    17.     //touch input control
    18.  
    19.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    20.     if (Input.GetMouseButtonDown(0))
    21.     {
    22.         if (Physics.Raycast (ray, hit , Mathf.Infinity))
    23.         {
    24.        
    25.             if (hit.collider.tag == "breakable")
    26.            
    27.             {
    28.    
    29.                 audio.Play();
    30.                 coin+=1;
    31.                   PlayerPrefs.SetInt ("Coin", coin);
    32.            
    33.         }
    34.        
    35.    
    36.    
    37.         }
    38.         }
    39.         }
    and heres my next level Gameover
    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4. public var textStyle: GUIStyle;
    5. static var coin:int=0;;
    6. function Start () {
    7.  coin = PlayerPrefs.GetInt("Coin");
    8. }
    9.  
    10. function Update () {
    11.  
    12.  
    13.  
    14. GUI.Label( Rect(250,75,25,25), String.Format("{0}", coin),textStyle);
    15.  
    16.  
    17.  
    18. }
     
  6. iMob

    iMob

    Joined:
    Apr 9, 2014
    Posts:
    55
    Use your gui code in OnGUI(), not in Update() and it works.

    Code (csharp):
    1.  
    2.    function OnGUI() {
    3.         GUI.Label( Rect(250,75,25,25), String.Format("{0}", coin),textStyle);
    4.     }
    5.  
     
    Last edited: Apr 13, 2014