- Home /
How do i use a value from a var
Hello, I am having trouble getting a object to spawn in once i hit a certain value, This is what i am trying to do...
var rank = 0;
function Update () {
if (rank == 5){
Debug.Log("We unlocked first item");
}
}
I replaced everything with a Debug to see if its working But it dose not seem to work out when i change the value to 5, Any help would be greatly appreciated. Thanks.
Answer by save · May 23, 2012 at 11:08 AM
Perhaps you could try something like this instead:
#pragma strict
private var rank : int = 0;
function Start () {
InvokeRepeating("RankUp",1,1); //For testing purposes, use RankUp() when condition for ranking up is met
}
function RankUp () {
rank++;
switch (rank) {
case 1: Debug.Log("Rank is now "+rank); /*Give special values for rank 1*/ break;
case 2: Debug.Log("Rank is now "+rank); /*Give special values for rank 2*/ break;
case 3: Debug.Log("Rank is now "+rank); /*Give special values for rank 3*/ break;
case 4: Debug.Log("Rank is now "+rank); /*Give special values for rank 4*/ break;
case 5: Debug.Log("Rank is now "+rank); /*Give special values for rank 5*/ break;
default: Debug.Log("Rank is now "+rank+" which is over the rank limit"); break;
}
}
The problem you had is most likely because Unity has serialized the value for rank and put it in the Inspector, changing the value inside the script wont make any difference. Remember to type all your variables (`var rank : int = 0`).
Thanks allot works perfect!
1 other thing, Whats #pragma strict, Everything seems to work without it,
Thanks again.
Good thing! #pragma strict is disabling dynamic typing, which is crucial for optimizing performance. If you just type your variables (and returning functions) then you'll most likely be good to go.
Sorry i just realized something, This all works perfect apart from it checks every 1 frame "InvokeRepeating("RankUp",1,1);"
I have it so a gameObject gets activated for example
private var rank : int = 0;
function Start () {
InvokeRepeating("RankUp",1,1);
}
function RankUp () {
//rank++;
switch (rank) {
case 10:
Helicopter.SetActiveRecursively(true);
break;
default: Debug.Log("Rank is now "+rank+" which is over the rank limit");
break;
}
}
So every 1 frame at Rank 10 it is trying to activate the helicopter causing lag spike at every 1 frame, Sorry if there is a simple solution i just cant think of any, Coffee dose not seem to do it for me any more.
Thanks.