Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Crucible1 · Jan 16, 2016 at 06:43 PM · canvascubeclickmovieclickable

Can you use a cube to bring up a UI?

Any help with this will be super appreciated because I am getting no where on my own.

What I want to do is click on a cube which will bring up a UI canvas, which will then play a movie. So far I can click and bring up the canvas (with my movie playing), but I can click anything and it will bring up the canvas.

This is my script:

public class ClickCube : MonoBehaviour {

 Canvas canvas;

 public GameObject Clickcube;

 void Start()
 {
     canvas = GetComponent<Canvas>();
     canvas.enabled = false;
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         Pause();
     }
 }

 public void Pause()
 {
     canvas.enabled = !canvas.enabled;
     Time.timeScale = Time.timeScale == 0 ? 1 : 0;
 }

}

At this point I am beginning to wonder if using a cube to bring up a UI is even possible! Also, I'm pretty new to Unity so any help at all will be amazing. Thank you!

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
1

Answer by corn · Jan 17, 2016 at 05:39 PM

You misunderstood how Input.GetKeyDown works.

      if (Input.GetKeyDown(KeyCode.Mouse0))
      {
          Pause();
      }

This will be called whenever you left-click, not when you left-click on the GameObject this script is attached to.

To detect if you actually clicked your GameObject, and not just a random point of the screen, you'll have to use a Raycast.

First, you detect a click, just like you already do, then you check what you hit with that click.

     void Update () 
     {
         if (Input.GetMouseButtonDown(0))
         {
             // Create a ray going from camera to the screen point 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
                 
             // Check if the ray hits a collider
             if (Physics.Raycast(ray, out hit))
             {
                 // Check if the collider we hit is attached to the current GameObject
                 if (hit.collider.gameObject == gameObject)
                 {
                     Pause();
                 }
             }
         }
     }

Don't forget to add a Box Collider to your cube, and you're good to go.

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 Crucible1 · Jan 18, 2016 at 11:08 AM 0
Share

Thanks so much for getting back to me!

I put your code in, but when I click on the cube this error comes up:

NullReferenceException: Object reference not set to an instance of an object OnClick.Play () (at Assets/Script/OnClick.cs:36) OnClick.Update () (at Assets/Script/OnClick.cs:27)

So I changed the coding slight to try and directly link in the UI that I want to bring up (as I figured that was what the error was about). I made this change:

// Check if the collider we hit is attached to the current GameObject if (hit.collider.gameObject == gameObject) { GetComponent(); Pause();

But I still get the same error.

Any advice? Sorry if I'm missing something really obvious. Thank you.

avatar image Crucible1 · Jan 18, 2016 at 02:17 PM 0
Share

I got it to work now! Here is what I did:

public class OnClick : $$anonymous$$onoBehaviour

{

 Canvas canvas;

 void Start()
 {
     canvas = GetComponent<Canvas>();
     canvas.enabled = false;
 }

 void Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
     {
         // Create a ray going from camera to the screen point 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;

         // Check if the ray hits a collider
         if (Physics.Raycast(ray, out hit))
         {
             // Check if the collider we hit is attached to the current GameObject
             if (hit.collider.gameObject == gameObject)
             {
                 Play();
             }

         }
     }
 }

 public void Play()
 {
     canvas.enabled = !canvas.enabled;
     Time.timeScale = Time.timeScale == 0 ? 1 : 0;
 }

}

I think I just missed out bit from my original script when incorporating your one. Thank you so much for your help, this has been driving me crazy for way too long.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I change the UI button color when clicked 1 Answer

Playing a .mp4 video file after Scene change without Unity Pro? 0 Answers

How to order clickable gameObjects that overlap? (2D) 1 Answer

How can I change image when click Icon? 1 Answer

Multi-Display Click Detection 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