Is it possible to make a public variable in javascript?
Because i have this code
function OnCollisionEnter(collision : Collision)
{
//collision code
if(collision.gameObject.tag == "EnemyHit"){
Debug.Log("OOPS: Player fell into the void!");
health.LIVES -= 1;
public var flash = true;
yield WaitForSeconds(4);
}
}
And gets this error
Assets/Scripts/Player/ChangeLife.js(8,17): BCE0044: expecting }, found ‘public’.
public var flash = true;
function OnCollisionEnter(collision : Collision)
{
//collision code
if(collision.gameObject.tag == "EnemyHit"){
Debug.Log("OOPS: Player fell into the void!");
health.LIVES -= 1;
yield WaitForSeconds(4);
}
you can, but if you create a variables inside a function it will be local to that function only. If you put outside it will be global anyone can access it.
For example if you’r using public you need to put outside function but if you want to put it inside a function just put var
example:
function OnCollisionEnter(collision : Collision)
{
var flash = true;
//collision code
if(collision.gameObject.tag == "EnemyHit")
{
Debug.Log("OOPS: Player fell into the void!");
health.LIVES -= 1;
yield WaitForSeconds(4);
}
}
There can also only be 1 static named flash though. What you should do is to consider using either SendMessage to pass information between scripts or referencing the script on the other object.
I dont know if i’m right here, but if i’m wrong someone please correct me. I think this happen because your Rigidbody is still colliding with the “EnemyHit”. So this will make your lives change more than once before run the yield action. If im not mistaken after the 4 second yield your lives will drop some more. Am i right?
Static + Constant: Both only have one instance per script (so Enemy.health - if a constant or a static variable is shared between ALL instances of Enemy, which is clearly not desired)
Static: This is a variable, it’s value can be changed (Enemy.health -= 100 == no problem)
Constant: This is a constant, it’s value cannot be changed (Enemy.health -= 100 == compiler error)
EDIT:
Forgot one really important part - constants can only hold value types where as static variables can hold anything. If you want a static variable to behave more like a constant, you can make it a “static readonly” (or a static variable and a static property with no setter).
OnCollisionEnter is going to be called every time something collides with you - you need to check “flash” before you continue. So maybe something like this:
So now we are checking to make sure flash is currently false before we do anything, additionally - after we yield for four seconds we set flash back to false (so that we can be hit again).
It’s a pretty messy way to do it, but it should work. You should only ever use static variables if you only intend for one instance of that class to be in the game though.
Not necessarily. You could have 100’s of objects of the same type but if you want them to all reference the same value, then a static variable would suffice.