Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Eowyn27 · Dec 31, 2013 at 05:40 PM · c#onguionmousedownlabelgui.box

How do I make onMouseDown work for OnGUI in C#?

Currently I have the js version:

 var _mouseDown = false;
 function OnGUI() {
 if(_mouseDown) {
 
  GUI.Box(Rect(10,10,100,20),"object!");
 
 }
 }
 function OnMouseDown () {
 _mouseDown = true;
 }
 function OnMouseUp () {
 _mouseDown = false;
 }
 
 function OnMouseOut() {
 
 _mouseDown = false; 
 }
  

However, I can't seem to get the same thing for C#. When I try to rewrite it with void onMouseDown, I get errors. Here is a sample from the reference manual that I want to use but I can't seem to add onMouseDown properly to get it to work.

 using UnityEngine;
 using System.Collections;

 public class TextLabelC : MonoBehaviour {
     
 void OnGUI() {
     GUI.Box(new Rect(10, 10, 100, 20), "Object");
 
 }
 

}

Also, for my above script in js, I am wondering how do I make the label stay on the screen without having to hold and press the mouse down but also, having it disappear when navigating to another object

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by YoungDeveloper · Dec 31, 2013 at 05:43 PM

Hi, If i remember correctly you can use Event handler.

 if (Event.current.type == EventType.MouseDown){
 
 }

You can even pre save it if you need to use it many times.

 Event e = Event.current;
 if (e.type == EventType.MouseDown){
 
 }
Comment
Add comment · Show 5 · 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 Eowyn27 · Dec 31, 2013 at 06:37 PM 0
Share

Error CS1519: Invalid token 'if' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp-firstpass) Error CS1519: Invalid token '==' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp-firstpass) Error CS1519: Invalid token ')' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp-firstpass)

Those are the errors I get with your code.

avatar image YoungDeveloper · Dec 31, 2013 at 06:49 PM 0
Share

:D Did you just copied it inside class ? It should be in OnGUI() method.

 void OnGUI(){
     Event e = Event.current;
     if (e.type == EventType.$$anonymous$$ouseDown){
         Debug.Log("press");
     }
 }
avatar image Eowyn27 · Dec 31, 2013 at 07:17 PM 0
Share

Oh I see what you mean but for some reason, I can't seem to get the GUI.Box to show on the screen. Nothing appears when I click.

avatar image YoungDeveloper · Dec 31, 2013 at 07:23 PM 0
Share

This will toggle (show and hide) GUI box on mouse press

 private bool toggleBox = false;
 
 void OnGUI(){
     if(toggleBox)GUI.Box(new Rect(10, 10, 100, 20), "Object");
 
     Event e = Event.current;
     if (e.type == EventType.$$anonymous$$ouseDown){
         toggleBox = !toggleBox;
     }
 }
avatar image Eowyn27 · Dec 31, 2013 at 07:34 PM 0
Share

Ins$$anonymous$$d of hard coding the object name, is it possible to assign new labels for each object I click on? (If I moved from Object 1 to Object 2 and I want each label to display different names for each object selected). How would I get started on something like that?

Thank you so much again!! :D

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.

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

I keep getting an error on my for loop. 1 Answer

Making scrollbar for GUI.Label inside a draggable GUI.Window 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Do something when there is no mouse click 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges