Search Unity

Variable problems

Discussion in 'Scripting' started by piotrek2408, Mar 28, 2015.

  1. piotrek2408

    piotrek2408

    Joined:
    Mar 27, 2015
    Posts:
    9
    Hi.
    I have some problems with variables.
    I have two: points = 200 and score.
    I would that variable points will start in each round from 200 but with every 1 second, decrease by 10.
    After click in any button, decreasing will stop. And I want to add value of variable points (value from this moment when decreasing will stop) to variable score (the same in every round). I've been searching for answer on my questions and I've tried a lot of solutions but nothing works.

    There is one more thing that I can't held.
    I want to situate value of variable Points at screen as text. Another advices don't work too.

    Pls, help.
    How can I solve my problems?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I'm currently on my mobile device so typing out code isn't an option, but here's some advice for guidance.

    You're already on the right track. You need a function that runs every second (look into coroutines) and ticks points away from the possible gain. That function should check a variable to see if it should be continuing the deduction or not. Your buttons should trigger that variable to be false.

    The total score should either be a playerpref, a static variable, or even better a member of a singleton game manager object. Search for those terms to evaluate the options and see how to implement them.

    Keep at it!
     
  3. piotrek2408

    piotrek2408

    Joined:
    Mar 27, 2015
    Posts:
    9
    Ok, thanks. I'll try your advices.
    There's one more thing without solution: how to place value of variable as text on the screen?
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Unity's UI has a text object type. Throw one of those into your scene, store it as a public variable in your script, and change its 'text' property to be the value of the score variable.
     
  5. piotrek2408

    piotrek2408

    Joined:
    Mar 27, 2015
    Posts:
    9
    Ok. I wrote this:
    Code (CSharp):
    1. public class zmienne : MonoBehaviour {
    2.  
    3.     int points = 200;
    4.  
    5.  
    6.  
    7.     IEnumerator Start ()
    8.     {
    9.         points = Decreas(points);
    10.         Debug.Log (points);
    11.         yield return new WaitForSeconds(1);
    12.     }
    13.  
    14.  
    15.  
    16.     int Decreas(int number){
    17.  
    18.         int ret;
    19.  
    20.             ret = number - 10;
    21.             return ret;
    22.     }
    23. }
    But variable "points" is still 190. What I did wrong?
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You're getting there, but I don't think you understand coroutines just yet. I haven't tested this, but it should lead you down the right path, at least.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class zmienne : MonoBehaviour {
    5.  
    6.     private int possiblePoints = 200;    // For tracking total possible points this round
    7.     private bool keepGoing;                // Should we keep taking points away?
    8.  
    9.     public bool buttonClicked = false;    // TODO - Just an example. Change this in the inspector to stop the clock
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         keepGoing = true;
    14.         StartCoroutine(DecreasePoints(10, 1.0f));
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (buttonClicked) {
    20.             keepGoing = false;
    21.         }
    22.     }
    23.  
    24.     IEnumerator DecreasePoints(int pointLossAmount, float pointLossRate) {
    25.         // This loop will keep running while keepGoing is true
    26.         while (keepGoing) {
    27.             possiblePoints -= pointLossAmount;
    28.             yield return new WaitForSeconds(pointLossRate);
    29.         }
    30.  
    31.         // If you've reached this point, it means a button was clicked
    32.         // TODO - Handle adding possible points to total score
    33.     }
    34. }
    Additionally, I'm not sure if you're allowed to make the Start method a coroutine, as it's already defined in MonoBehavior. While this isn't the source of your problem, unless you have a really good reason to, you probably shouldn't try to overwrite Start.

    The real benefit a coroutine provides you is performing a task iteratively, while giving up its control to the rest of your code so you can keep doing other things concurrently. To do this, the coroutine needs some type of repeating control structure (a loop) to be iterating over. Yours was just running once because that's all you were telling it to do.

    Don't forget, in order for this to be saved between sessions, your total score variable would need to be managed as I described earlier.