- Home /
Objects act like buttons
Hi,
I want to make a object a button so when it's pressed it will run a script. How do I do this?
Thanks
Answer by Lovrenc · Jan 30, 2013 at 09:28 AM
For pc you add a script which has:
function OnMouseDown () {
//Do your stuff
}
For mobile devices you will have to use raycast:
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
if(hit.gameObject.Tag == "myCube")
//Do your stuff
}
Sir were should I put this code? sorry Im new in using c#
The provided code is not C#.
I suggest you the answer of the following question ins$$anonymous$$d:
https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do you trigger components from different objects? 1 Answer
Array out of range! 1 Answer
How do I make a button appear when my player enters a trigger? 1 Answer
I need to store the amount of objects in scene in a var. How? 1 Answer