Click on specific object with certain property only please help!
Hey everyone I just wondering if someone could point me in the right direction on how to accomplish this. I have 6 triangles in my game. They are all tagged with "TriangleP". They also all have Box Collider 2D. I created a script to randomize which triangle is assigned to a moving script (from itween):
public GameObject[] Triangles;
void Start () {
Triangles = GameObject.FindGameObjectsWithTag ("TriangleP");
Triangles [Random.Range (0, Triangles.Length)].AddComponent<MoveSample> ();
}
I also created a java script to detect mouse clicking on any of those triangles:
function Update () {
var hit: RaycastHit2D;
if(Input.GetMouseButtonDown(0)){
hit=Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition) , Vector2.zero);
if(hit.collider!=null && hit.transform.gameObject.tag=="TriangleP"){
Debug.Log("SUCCESS");
}
}
}
What I want to accomplish is a mouse click only registering (print success) when I click on the triangle that is moving. Is there anyway to accomplish this? Any help or hint in the right direction would be Greatly Appreciated! Thank you.
Answer by Oukalakakou · Mar 15, 2016 at 07:59 AM
Edit
In unityscript you can check a component on RayCast by doing the following where hit is your RayCastHit:
if(hit.collider != null){
if(hit.collider.GetComponent(YourComponent) != null){
Debug.Log("success");
}
}
The c# version would be:
if(hit.collider != null){
if(hit.collider.GetComponent<YourComponent>() != null){
Debug.Log("success");
}
}
Answer by Jalah_LLC · Mar 16, 2016 at 12:30 AM
ok thank you very much for your help! I added the following to the java script code:
var hit: RaycastHit2D;
if(Input.GetMouseButtonDown(0)){
hit=Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition) , Vector2.zero);
if(hit.collider.GetComponent(MoveSample)!= null && hit.transform.gameObject.tag=="TriangleP"){
Debug.Log("SUCCESS");
It does seem to work where the triangle moving is the only one registered with success. However, if I click anything else like outside the triangles (the camera view and I have the background default blue), I get this error:
NullReferenceException: Object reference not set to an instance of an object ClickOnObject.Update () (at Assets/Scripts/ClickOnObject.js:14)
its directing it to this line:
if(hit.collider.GetComponent(MoveSample)!= null && hit.transform.gameObject.tag=="TriangleP"){
is there something else I am missing? I would really appreciate it. Thanks!
Yes I should have warn you to use nested conditions in this case. And also noticed that you're using unityscript and not c#, sorry.
In fact the error come from unity trying to get the component of a null RayCastHit2D with obviously a null collider.
try something like this
if(hit != null){
if(hit.collider.GetComponent($$anonymous$$oveSample)!= null && hit.transform.gameObject.tag=="TriangleP"){
Debug.Log("SUCCESS");
}
}
This should prevent trying to access the component of a null reference, not entering the second condition when the raycast hit nothing.
So it looks like I got the same error when I put in the extra hit!=null line. Is there anything else you can think of? Or is there a different way to do this in C# compared to Javascript? Any help or thoughts would be greatly appreciated!
Try using hit.collider != null ins$$anonymous$$d of hit != null I was wrong to think that hit could be null but the collider certainly is when nothing is hit.
Your answer
Follow this Question
Related Questions
Is there a cleaner way to set all the properties when declaring a new object? 2 Answers
'Serialization' does not exist in the namespace 'Unity.Properties'? 0 Answers
Disable mouse click on gameObject 1 Answer
The difference between using variables from a struct and in a class. 0 Answers
DLL Properties grayed out (unselectable), unable to exclude plugin from platforms 0 Answers