- Home /
Add to score every second
Hey, I'm making a tycoon game and I want to increase the money every second but I can't figure it out.
Answer by Eric5h5 · Jun 24, 2011 at 04:07 PM
var money = 500;
var increase = 25;
function Start () {
InvokeRepeating("AddToMoney", 1.0, 1.0);
}
function AddToMoney () {
money += increase;
}
...hate how this new system doesn't warn you when some one posts a new answer. Looks like the answer I edited into $$anonymous$$e is exactly the same as the one you posted.. I guess great $$anonymous$$ds think alike :p but I'll delete $$anonymous$$e since you beat me be a few $$anonymous$$utes. ;)
The system has also stopped sending me notifications for anything.... (I'd rather it had in-site notifications like the old system had anyway.)
Email notifications work 'okay' for me (I'd say I get 80-90%). I meant the notification you'd used to get when you where in the process of answering a question of editing an answer and it said 'a new answer has been added to this question, do you want to load it in?'. And yeah, in-site notifications would be great.
Yes, I know, I was just adding to the complaint. :) Although right after I posted that, I started getting notifications again; figures.
i Tried this and its didnt work for me i did do my own changes but i tried the original code and it didn't work for me debug logs say "function" isn't working debug log goes as
"Assets/Scripts/Score.cs(15,3): error CS0246: The type or namespace name function' could not be found. Are you missing an assembly reference?" "Assets/Scripts/Score.cs(7,9): error CS0246: The type or namespace name
variable' could not be found. Are you missing an assembly reference?"
"Assets/Scripts/Score.cs(6,9): error CS0246: The type or namespace name variable' could not be found. Are you missing an assembly reference?" Code is below and i am using C# witch might be part of the problem
using UnityEngine; using UnityEngine.UI;
public class Score : $$anonymous$$onoBehaviour {
public variable TotalScore = 0;
public variable increase = 1;
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update () {
scoreText.text = InvokeRepeating("AddPoint", 1.0, 1.0);
}
function AddPoint () {
TotalScore += increase;
}
} `
Answer by christo1745 · Jun 24, 2011 at 04:03 PM
That would update the money amount every frame. To do it every second you could do:
function Update()
{
if(Time.time - lastUpdate >= 1f){
money += increase;
lastUpdate = Time.time;
}
}
Use the code button at the top (the 101010 button) to format your code, please...
Answer by Smorteza · Apr 24, 2013 at 11:57 PM
function Update() { if(Time.time - lastUpdate >= 1f){ money += increase; lastUpdate = Time.time-lastUpdate;
} }