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. Dismiss Notice

Where is the bug? Please Help

Discussion in 'Scripting' started by DS Technology, May 29, 2013.

  1. DS Technology

    DS Technology

    Joined:
    Nov 2, 2012
    Posts:
    12
    So will make a Highscore, but it don´t works.


    Can you complete the code for me?


    I have no idea what i will make.

    Thanks for every help :)


    --------------------------------------


    var target : Transform;
    private var myTransform : Transform;
    var currentDistance : String = "";

    var score : GUIText;
    var highScore : GUIText;

    var bestScore : float = 0;


    function Start () {
    myTransform = transform;


    if (PlayerPrefs.HasKey("Hightscoreab")){
    bestScore = PlayerPrefs.GetFloat("Hightscoreab");

    }


    }

    function Update () {
    var distance = (target.position - myTransform.position).magnitude;
    currentDistance = distance.ToString("F0");
    score.text = "Punkte: " + currentDistance;

    if (bestScore <= score) { /////here is a bug say unity

    PlayerPrefs.SetFloat("Hightscoreab", score)(); ////here is a bug say unity
    }


    }

    ////maybe are more bugs, thank you for every help
     
  2. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    You are checking and saving in the prefs "score" (that is a GUIText) instead of "distance" that is the float you want to check and save.
     
    Last edited: May 29, 2013
  3. oneXwork

    oneXwork

    Joined:
    Mar 3, 2011
    Posts:
    266
    Code (csharp):
    1.  
    2. PlayerPrefs.SetFloat("Hightscoreab", score);
    3.  
    Like SkaredCreations say you comparing the wrong variable and your playerprefs saving function is wrong it should not had an additional () at the back.
     
  4. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
    score is a GUIText - not a value, it's a whole object.

    PlayerPrefs.SetFloat("Hightscoreab", score.text);

    However, that's still a string, so even if it's just a number, it's actually still a string, not a float.

    Try (untested)...
    PlayerPrefs.SetFloat("Hightscoreab", float.Parse( score.text ) );