• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lasset · Feb 19, 2013 at 06:03 PM · guiclickonguionmousedown

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);
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 19, 2013 at 07:47 PM 0
Share

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.

avatar image Lasset · Feb 19, 2013 at 08:17 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Text popup onMouseDown 2 Answers

How to display a set of guiText OnGUI 0 Answers

OnGUI() Resources 1 Answer

[Solved] GUI Error? 1 Answer

Ignore Input.GetMouseButtonUp for clicks that were used on GUI elements 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges