true and false problems

In a game I am working on, the player is suposed to click on blocks to remove them
I used this code, but it wont work

Adding

yield WaitForSeconds (1);
ReadyNow = true;

in OnMouseDown wont work beacouse it stops as soon as ReadyNow becomes false

var PointOnDestroy = 100;

//the code will be aplied to more than one gameobject, so the bool has to be static
static var ReadyNow : boolean = true;

//Removing the cube when a player clicks it
function OnMouseUp () {
	if (ReadyNow == true){
	//Destroy the gameobject
	Destroy(gameObject);
//adding points to the score, I doubt the problem is here...
	Score.SCORE +=PointOnDestroy;

//making it imposible to click again before ReadyNow is true
	ReadyNow = false;
	}
}

//I guess the problem is that the function doesent update every frame, but calling the function Update produces the error: 
//Script error: Update() can not be a coroutine.
function ReturnToTrue () {
	if (ReadyNow == false){
//wait a second before making it possible to remove blocks again
	yield WaitForSeconds (1);
	ReadyNow = true;
	}
}
function OnMouseUp () {
	if (ReadyNow == true){
		
		Destroy(gameObject);
		
		Score.SCORE +=PointOnDestroy;
		
		ReadyNow = false;
		
		yield new WaitForSeconds (1);
		
		ReadyNow = true;
	}
}

Is there a problem with that ?

Beacouse the function stops when readyNow is false so it never gets to ReadyNow = true;, I have tried it

my dad helped me and we tried with this script:

var PointOnDestroy = 100;
static var ReadyNow : boolean = true;

function Update () {
	 if (ReadyNow == false) {
	 BecomeTrue ();
	 }
}

function OnMouseDown () {
	if (ReadyNow == true){
	//Destroy the gameobject
	Destroy(gameObject);
	Score.SCORE +=PointOnDestroy;
	ReadyNow = false;
	}
}

function BecomeTrue () {
yield new WaitForSeconds (5);
ReadyNow = true;
}

but insdead of waiting for 5 seconds, it waits for a ramdom number of seconds (between 0 and 10)

Did my answer at least helped you ? ._.

yes, you made the script mutch betther written, my version of the script was a mess:P
thanks:)

but I still have the same problem
do you have any idea why yield WaitForSeconds (5); waits a random amount of time instead of 5 seconds???

it does not wait a random amount of time, it waits exactly 5 seconds
your problem is the code in update thats simply wrong as you fire it over and over and over again.

the correct code would be to fire off the coroutine where you fire the weapon so it gets reset 5s after firing, not every few ms after the 5 seconds of firing

To start of, I just want to thank you both for being patient with me and helping me so much, you have learnt me a lot more about programming in unity:)

Do you mean to start the coroutine when the gameobject is destroyed like this or am I doing something completaly wrong?

var PointOnDestroy = 100;
static var ReadyNow : boolean = true;

function OnMouseDown () {
	if (ReadyNow == true) {
	BecomeTrue();
	Destroy(gameObject);
	Score.SCORE += PointOnDestroy;
	ReadyNow = false;
	}
}

function BecomeTrue () {
	Debug.Log("function started");
	yield WaitForSeconds (1);
	ReadyNow = true;
	Debug.Log("function ended");
}

by looking at the messages prodused by Debug.Log, it looks like it gets to yield WaitForSeconds, but never continues.

what am I doing wrong :face_with_spiral_eyes: ???

There are some cases that, when the game object is destroyed, the script can’t go on. Is it the case, dreamora ?

I have spent hours trying to make this work

why wont the script move past yield WaitForSeconds? How can I make it move past it? I am realy shure the problem lies in that one line

Do you get any runtime error messages? A function will be aborted at the point where an exception occurs.

AkilaeTribe is on the right path. You’re destroying the GameObject before your yield returns. It typically takes only a frame (so, roughly a few ms) for the object to be destroyed, so that instance isn’t getting to finish executing BecomeTrue. Unfortunately, you can’t use Invoke a call to a static method and starting a static coroutine doesn’t seem to work. If you really want to destroy the object after it’s been clicked (rather than just set it inactive indefinitely), you can set the object inactive until the invocation and then destroy it, eg:

function OnMouseDown () {
    if (ReadyNow == true) {
        Score.SCORE += PointOnDestroy;
        ReadyNow = false;
        Invoke ("BecomeTrue", 1);
        gameObject.SetActiveRecursively (false);
    }
}

function BecomeTrue () {
    ReadyNow = true;
    Destroy (gameObject);
}

thank you for trying to help but
I tried the script, but I got an error (BCE0044) that said that there was suposed to be a ( instead of BecomeTrue

is it a glitch in the compiler or something?

There is just a missing } at the end of OnMouseDown function.

Sorry, yes, I forgot to close the bracket to the conditional statement there. I’ve edited the code in my post and it should work now.

Chickens get roasted for less than this…

Missing braces are not a big deal anyway. :slight_smile: