I’ve been trying at this for hours, which is ridiculous. I just want to create a script to delay mouse clicks for three seconds after a mouse click happens.
What I should need to do:
create a global variable
set that variable to current time after mouse click
check time to see if time = variable + 3, then say ‘OK’
I’ve tried for a long time, and I just can’t seem to get it to work. I either get strange errors from Unity (such as !IsPlayingOrEditMode) and other weird complaints. I’ve tried reading as much of the documentation as I can, but it doesn’t help me with this at all.
Does anyone have any suggestions on how I can solve this? Also, on a broader scale, is there any way I can have less of a miserable time trying to get to grips with Unity? Is there any other forms of tutorial or documentation that would help me make better progress?
I’ve used C and Blitz3D in the past and never hit my head so often, it’s starting to make me question whether I can work with this.
function OnMouseDown () {
Invoke("Click", 3.0);
}
function Click () {
// Do clicky stuff here
}
Sorry to hear you’re having problems…I’m not sure what to suggest since I didn’t have issues like that, but different people have different approaches to things obviously.
GUIUpdate.js
static var updateInterval = 3.0;
static var updateTime = 0;
static var timeLeft : float;
function Update () {
if (timeLeft > 0){
timeLeft -= Time.deltaTime;
}
guiText.text = "ready to remove block in : " + GUIupdate.timeLeft.ToString("f1");
}
I ended up creating a script which has some global variables (not sure Unity calls them ‘static’, but hey…), and keeps these up to date (a little like the FPScounter.js).
ClickToRemoveObject.js
var detachChildren = false;
function OnMouseOver () {
// Checks that mouse is clicked and time duration has passed
if (Input.GetButton ("Fire1") GUIupdate.timeLeft <= 0.0) {
if (detachChildren) {
transform.DetachChildren ();
}
DestroyObject (gameObject);
GUIupdate.timeLeft = GUIupdate.updateInterval;
}
}
This adds the three seconds to the ‘timeLeft’ variable of the other script, and checks to see whether it is back down to zero before allowing it to work again.