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;
}
}
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.
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: