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?
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!
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?
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.
Ok. I wrote this: Code (CSharp): public class zmienne : MonoBehaviour { int points = 200; IEnumerator Start () { points = Decreas(points); Debug.Log (points); yield return new WaitForSeconds(1); } int Decreas(int number){ int ret; ret = number - 10; return ret; } } But variable "points" is still 190. What I did wrong?
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): using UnityEngine; using System.Collections; public class zmienne : MonoBehaviour { private int possiblePoints = 200; // For tracking total possible points this round private bool keepGoing; // Should we keep taking points away? public bool buttonClicked = false; // TODO - Just an example. Change this in the inspector to stop the clock // Use this for initialization void Start () { keepGoing = true; StartCoroutine(DecreasePoints(10, 1.0f)); } // Update is called once per frame void Update () { if (buttonClicked) { keepGoing = false; } } IEnumerator DecreasePoints(int pointLossAmount, float pointLossRate) { // This loop will keep running while keepGoing is true while (keepGoing) { possiblePoints -= pointLossAmount; yield return new WaitForSeconds(pointLossRate); } // If you've reached this point, it means a button was clicked // TODO - Handle adding possible points to total score } } 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.