- Home /
How to make a GameObject a button?
I'm developing a game in which you have to find a button in a room, for the script i'm using this and just putting it on the button
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class OnClickLoadNextLevel : MonoBehaviour {
void OnMouseUpAsButton() {
SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex + 1);
}
}
but this creates some weird bug where you can't be too close to the button to click it, and it sometimes takes a few clicks to find the point at which it works, is there any other way i could do this? If you wan't to see the bug yourself go to ►http://gamejolt.com/games/find-the-button/155300
I didn't get past level 4, but other than that the button always behaved like I expected it to.
If you look at a few of the LP's in the comments you can see others have the same problem, it happens with every build, try to get close to the button
Answer by Brijs · Jun 13, 2016 at 07:06 AM
Use OnMouseDown() method of monobehaviour
It will be called when you click gameobject
Write your code in this method
reference : OnMouseDown
This still has the bug happen, is there another way or do i have to just suck it up lol
can you share us the bug? On$$anonymous$$ouseDown looks perfectly fit to your project.
it won't work if you're too close to the button or sometimes just doesn't work completely.
Answer by noorudheen · Jun 14, 2016 at 03:29 AM
in ufps demo, there is a button implementation. i think they are using raycast.
How would i use raycast for this? send a ray from the mouse and detect if the name is Button?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Enable/Disable part of script with Button? 0 Answers