- Home /
Create clickable GameObjects (JS)
I have a 2d board of some hundreds of board pieces, and I need to make ALL of them clickable. How would I do it with possibly GUIButtons or some other way to do it?
I have the code for what happens when the buttons are clicked, but I just need a way to make them clickable.
This is in a 2D JS environment.
Thanks in advance!
P.S. I've seen similar Q&As about this, however none of the raycasting code works.
P.P.S. My last question was deleted for no reason. PLEASE STOP!
Answer by 767_2 · Oct 11, 2014 at 10:27 PM
your object need collider and your camera must be set as main
function Update() {
if(Input.GetMouseButton(0)){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit,1000)) {
if (hit.collider.gameObject.tag=="something") {
Debug.Log("Success");
}
}
}
}
What kind of collider? Also, how would I set the collider positions dynamically?
if it is a box , box collider if sphere , sphere collider etc when you add collider to your prefab it will be with your object it doesnt need to set anything
I'm getting an "Object Reference not set to an instance of an object" error
I've fixed the error by removing the $$anonymous$$athf.infinity bit, but the code isnt being triggered. I have debugging, but nothing is showing up.
Your answer
Follow this Question
Related Questions
[SOLVED] Only instantiating once 1 Answer
GetComponent() problems 2 Answers
Detect Object that are hit by a ray 3 Answers
Scripts wont work if deactivated then activated? 0 Answers
Creating a teleportation gun 1 Answer