- Home /
Display message when OnMouseDown
Hello guys,
I have been searching how to display a message replacing the Debug.Log (code below) for a GUI during 2 seconds and then hide it. The research conducted has pointed out that I should create a boolean but when I try with the following code, I have an error that says "Return type 'void' cannot be used on a generator. Did you mean 'IEnumerator'? You can also use 'System.Collections.IEnumerable' or 'object'.. " Also, the object clicked is not destroyed.
Thank you in advance for your help.
var object: GameObject;
var information: String = "Well done";
var guiOn = false;
private var rect: Rect;
function OnMouseDown():void
{
var clickedTag:String = gameObject.tag;
if(Inventory.objectToBeFound == 1) selected = true;
guiOn = true;
rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
yield WaitForSeconds(5);
guiOn = false;
start.score += 100;
Debug.Log("Well done!");
Destroy(gameObject);
if(Inventory.objectToBeFound != 1) {
Debug.Log("incorrect object");
}
}
function OnGUI(){
if (guiOn){
GUI.Label(rect, information);
}
}
Answer by robertbu · Feb 19, 2013 at 06:30 PM
Remove the ':void' in this statement:
function OnMouseDown():void
The rest of the code looks like it should work.
Answer by Lasset · Feb 19, 2013 at 07:36 PM
Thanks for the fast answer. You're right, the error disappears and I get the display message in the first "if". Do you know how can I generate another message for the second "if"? I have tried the code below but didn't work.
Thanks!
var object: GameObject;
var information: String = "Well done";
var information2: String = "incorrect object";
var guiOn = false;
var guiOn2 = false;
private var rect: Rect;
function OnMouseDown()
{
var clickedTag:String = gameObject.tag;
if(Inventory.objectToBeFound == 1) selected = true;
guiOn = true;
rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
yield WaitForSeconds(5);
guiOn = false;
HudGame.score += 100;
Debug.Log("Well done!");
Destroy(gameObject);
if(Inventory.objectToBeFound != 1) {
guiOn2 = true;
rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
yield WaitForSeconds(5);
guiOn2 = false;
Debug.Log("incorrect object");
}
}
function OnGUI()
{
if (guiOn) {
GUI.Label(rect, information);
}
if (guiOn2) {
GUI.Label(rect, information2);
}
}
Answers should be reserved for potential answer to your question. Use the "add new comment" button below your question to add new material.
As for your problem. You destroy your game object on line 21, so the rest of your code will not execute. I'm not sure what you want to happen in case the Inventory.objectToBeFound != 1, but at the very least you need to be the Destory() below your second if statement to get this code to work.
I didn't know about the potential answers, sorry.
I have removed the Destroy() from line 21, as you recommended. Related to the Inventory.objectToBeFound != 1, if the object clicked is not object number 1 but 2 or 3 (other objects inside the array) I want a display message again which says "Object incorrect".
Your answer
Follow this Question
Related Questions
Text popup onMouseDown 2 Answers
OnGui Function help 1 Answer
Draw movetool gizmo in OnDrawGizmos() 1 Answer
My OnGUI() Won't show the Button elements :( 0 Answers
Drawing a GUI.DrawTexture call above GUI.Button in seperate scripts 0 Answers