- Home /
Accessing colliders, in if statement
Hello! Just to be clear i am using Javascript. I have been working on trying to detect if my mouse has clicked on a specific collider if it is clicked on that specific collider.
heres my code:
var A1L1 : GameObject;
function Update () {
if (Input.GetMouseButtonDown (0)) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit:RaycastHit;
if (Physics.Raycast (ray, hit)) {
Debug.Log("HIT SOMETHING!");
if (hit.collider == A1L1) {
Debug.Log("only detected level 1 hit!");
}
}
}
}
My problem is in the "IF statement" that deals with hitting the specific collider, it is not currently accessing the collider on that game object called A1L1, and is NOT returning Debug.Log("only detected level 1 hit!"); in my console!
Thanks for the help Daniel
Answer by clunk47 · Jul 22, 2013 at 07:11 PM
Needs to be
if(hit.collider.gameObject == A1L1)
{
Debug.Log("Your message here");
}
This is because you defined A1L1 as a GameObject. If you want to just ask if(hit.collider == A1L1), you need to define A1L1 : Collider;
Answer by amphoterik · Jul 22, 2013 at 07:09 PM
As a sidestep to your question, it seems you are trying to allow the player to click an object. You can put a script on the object and use the OnMouseDown() function to detect the click:
function OnMouseDown () {
//do something
}
source: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html
Thanks Great answer but that wasn't my issue. Thanks anyways!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to block dragging if collider hits 1 Answer
Boolean wont acitvate if the enemy enters a trigger 1 Answer
Help with Enemy AI 1 Answer
Melee-like function? 1 Answer