- Home /
Would like a function to change a parameter once, after an "if" statement has been met.
I've tried using "function Update ()" but that updates every frame until the parameter changes from the if statement. I'm trying to make the lives increase by 1 when the score gets to a certain amount.
static var score: int;
static var lives: int;
function Start () {
score = 0;
lives = 3;
}
function OnGUI () {
GUI.Box (Rect (10,10,90,30), "Score: "+score);
GUI.Box (Rect (Screen.width - 100,10,90,30), "Lives: "+lives);
}
function Update () {
if (score == 20)
{
lives++;
}
}
I've looked up multiple ways to try and do this, but I'm a complete noob at javascript. I've heard to use coroutines, bool, and other things, but I don't even know where to start with those. I even read about using invoke, awake, and the like, but I need it to change when the score reaches a certain amount, not during a timeframe. Thank you in advance, and any links/tuts would be greatly appreciated as this is my first week scripting.
I suppose another script accesses the "score" variable and increment it ?
What behavior do you exactly want ? When the score is 20, one more life, and then what ? the score starts over to 0 ? Or the score has to be 40 then to win another life ?
Answer by WhoRainZone1 · Jul 09, 2014 at 03:41 PM
There are several ways, the way I'd go is to add a new bool variable like this
static var score: int;
static var lives: int;
var reachedMaxScore: bool;
And also modify your Update function this way
function Update () {
if (score == 20 && reachedMaxScore == false)
{
reachedMaxScore = true;
lives++;
}
}
Cheers
Thank you so much. It worked like a charm. I kind of figured I needed to use a boolean, but had no idea how to implement it. Thanks again. Also, the new variable "reached$$anonymous$$axScore" you added, that is automatically assumed to be false?
Yes, but you should initialize it at false to be clear. But in that case the player can't win another life after that.
As $$anonymous$$iraSensei said, you should make it false in the Start() function to proper work.. I have a little sleep deficit due to heavy unity sessions, so I forgot to mention that. :)
Your answer
Follow this Question
Related Questions
Update increment error (2 + 1 = 0?) 1 Answer
How to Update a score count going up and down 1 Answer
Update Function Work Around Question 2 Answers
Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers
Script is causing immense lag, and I don't know what's causing it. 2 Answers