• 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 JHbett · Feb 25, 2015 at 08:15 PM · scripting beginner

Using OnMouseDown (or similar) to toggle worldspace UI (4.6)

I'd been using the following c# script to make a level select menu appear when a door in the scene is clicked, and disappear when clicking anywhere else.

 using UnityEngine;
 using System.Collections;
 
 public class Door_click : MonoBehaviour {
 
     private bool toggleBox = false;
     
     void OnMouseDown (){
         toggleBox = true;
         
         Debug.Log ("clicked on door!");
     }
     
     void OnGUI(){
         
         if (toggleBox)
             // Make a background box
             GUI.Box (new Rect (Screen.width/2, Screen.height/2 - 100, 200, 170), "Select room");
         
         
         if (toggleBox)
             // Make the first button.
         if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2 - 70, 180, 25), "Room 1")) {
             Application.LoadLevel ("scene_1");
         }
 
         if (toggleBox)
             // Make the second button.
         if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2 - 35, 180, 25), "Room 2")) {
             Application.LoadLevel ("scene_2");    
         }
 
         if (toggleBox)
             // Make the third button.
         if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2, 180, 25), "Room 3")) {
             Application.LoadLevel ("scene_3");    
         }
 
         if (toggleBox)
             // Make the fourth button.
         if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2 + 35, 180, 25), "Room 4")) {
             Application.LoadLevel ("scene_4");    
         }
 
         Event e = Event.current;
         if (e.type == EventType.MouseDown) {
             toggleBox = false;
         }
     }
 }

I'm completely new to any type of programming, so it's probably really messy, but at least it was working.

I'm now trying to do the same thing with the new UI, but I'm completely stuck.

How do I write a script, that can be attatched to my door, to make a different game object (in this case the canvas with all the buttons on) appear and disappear?

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

Answer by Mmmpies · Feb 25, 2015 at 08:33 PM

Just reference the base canvas and use SetActive.

 using UnityEngine.UI;    // add this to the top
 
 public Canvas myCanvas;  // drag teh canvas on here
 
 
 
 void OnMouseDown()
 {
     myCanvas.gameObject.SetActive(true);
 }

You can also Add Component and give the base canvas a Canvas Group and hide the canvas using that if you don't want to use SetActive.

Comment
Add comment · Show 4 · 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 JHbett · Feb 25, 2015 at 08:59 PM 0
Share

How do I drag objects from Unity into $$anonymous$$onoDevelop?

avatar image Mmmpies · Feb 26, 2015 at 07:35 AM 0
Share

Sorry hit night time where I am. You dont, put the script onto a gameObject. In this case the door so that On$$anonymous$$ouseDown works, when you highlight that object look in the inspector and the script will be referenced in there. Public variables will be shown so that canvas will appear as an empty slot drag your canvas from the hierarchy to that slot.

Now the best advice I can give is click on the Learn button above and look on Live Training for the tutorial on communicating between scripts and GameObjects it's a great tutorial.

avatar image JHbett · Feb 26, 2015 at 11:43 AM 0
Share

Oooh, I get it. I've got it working now, thanks so much for the help.

Yeah, I've been looking at those tutorials for some of the stuff I've been trying to do, but I need to go through them all properly when I have the time. At the moment I'm just flailing around trying to get my current project semi-functional for a deadline.

avatar image Mmmpies · Feb 26, 2015 at 11:51 AM 0
Share

Glad you got it working, also worth looking into the event system as it's mighty powerful.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

enable script on trigger 1 Answer

Enemy AI problems 1 Answer

Want a script that can instantiate an object when MOVING inside the trigger 1 Answer

How do I delay instantiating a prefab? 1 Answer

Freeze (lock) FPS position while still able to 'look around' 0 Answers


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