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!
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.
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.
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
