public var?

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’.

you have to do it like this:

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);
}

Oh, so it is because i cant make variables in functions?

So what shall i write to a other script, to see the variable?

i have tried this

 if(flash)

  {

  

  }

and gets this

Assets/Scripts/Player/Moving.js(10,6): BCE0005: Unknown identifier: ‘flash’.

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);
	}
}

Okay,

Thanks for the help, but i have still some problem.

when i have made the varieble

public var flash = true;
in script1,

And i want script2, to “find” the public var flash, to use in script2? in “if” command

So i shall write

if(what.in.here??:S)
{

}

that you have to used “static”, “static” will share the var between script.

script1:

static var flash : boolean = true;

function update()
{
  "do something here"
}

script2:

function update()
{
    if(flash == true)
    {
        "Do something here"
    }
}

This is my first script (changelife)

static var flash : boolean = false;

function OnCollisionEnter(collision : Collision)

 {



    //collision code

	if(collision.gameObject.tag == "EnemyHit"){

		Debug.Log("OOPS: Player fell into the void!");

		health.LIVES -= 1;

		flash = true;

		yield WaitForSeconds(4);

        }

}

This is my secound script (Moving)

function Update () {

  var horiz : float = Input.GetAxis("Horizontal");

  

  

  if(Input.GetButton("Horizontal"))

  {      

 transform.Translate(Vector3(horiz *0.3,0,0));

 transform.position.x = Mathf.Clamp(transform.position.x, -2.246169, 2.3);

  }

  if(flash == true)

  {

   Debug.Log(flash);

  }

  

}

but i still getting this error

Assets/Scripts/Player/Moving.js(12,14): BCE0005: Unknown identifier: ‘flash’.

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.

my bad @MrAwesome, you need to tell what *.js filename that have the static var

function update()
{
    if(script1.flash == true)
    {
        "Do something here"
    }
}

or you can do it like this

function update()
{
    if(GetComponent.<script1>().flash == true)
    {
        "Do something here"
    }
}

or

var object : GameObject; //gameobject that have the script1

function update()
{
    if(object.flash == true)
    {
        "Do something here"
    }
}

Thanks For the help :wink: Hizral84 :wink:

I got a next problem

the script

static var flash : boolean = false;



function OnCollisionEnter(collision : Collision)

 {



    //collision code

	if(collision.gameObject.tag == "EnemyHit"){

		

		health.LIVES -= 1;

		flash = true;

		yield WaitForSeconds(4);

        }

}

The problem is that it change the health more than once, before there have go 4 secounds why=

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?

question, does “static” make the variable a constant? or just make it accessible to outside scripts?

Static is similar to a constant.

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:

function OnCollisionEnter(collision : Collision)

 {



    //collision code

	if(flash == false  collision.gameObject.tag == "EnemyHit"){
		health.LIVES -= 1;
		flash = true;
		yield WaitForSeconds(4);
                flash = false;
        }
}

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.

Thanks all, and thanks KyleStaves, now it works