- Home /
The question is answered, right answer was accepted
Activate line just one time
Hi , I need help. How can I activate a line just one time , until the "if" changes , like a GetKeyDown but for vars, for example(JS);
example: boolean;
function Start () {
example = true;
}
function Update () {
if(example == true)
{
//I need this Log shows in the console just one time, until I change to false
//And true later
//Like press GetKeyDown to do only a thing each time
Debug.Log("Just one time");
}
}
If I use this script, the Log repeats every time it is read. Probably this can be the easier question you'll see today, but I rlly need help!
Answer by Nazirzadeh · May 16, 2016 at 05:58 AM
Is this what you mean?
if(example == true)
{
example=false;
if( Input.GetKeyDown( KeyCode.Space ) )
example=true;
Debug.Log("Just one time");
}
Not really, "Get$$anonymous$$eyDown" its only an example , because he play the next line just one time, here is the console when i play the first script (see the img), The Log is running all the time, i need just one,Today I´ve already thinked in other ways to solve my problm , like
var example: boolean;
var time: int;
function Start () {
example = false;
time = 0;
}
function Update () {
if(example == true)
{
// 1 - This int will increase
time = time + 1;
}
else if(example == false)
{
//3.2 - Only a way to reset
time = 0;
}
if(time == 1)
{
// 2 - This will happen until "time" is 1, in another update he will be two
Debug.Log("Just one time");
}
else if(time > 3)
{
// 3.1 - This is only a way to reset
example = false;
}
}
But if you meet any command that can simplify this script, will be very helpful, i'm sorry if you can't understand, I'm not american and my english scks.
![alt text][1] [1]: /storage/temp/70171-screenshot-1.png
Sorry but it is very bad coding style :)
static var justOne:boolean=true;
if(time == 1 && justOne==true)
{
justOne=false;
Debug.Log("Just one time");
}
You can set the justOne variable to false or true from the other scripts too, because its a static variable.
Sorry, I am very newbie in this area :p , but , tnks for the help!!