- Home /
How can stuff not be working while it works in another script....
Hello everyone!
It looks like I just made one dumb mistake but heres the code that stands in 2 c# scripts but is working in only 1 script:
if(Input.GetMouseButtonUp(0)){
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Collider2D hitCollider = Physics2D.OverlapPoint(mousePos);
if(hitCollider){
if(hitCollider.name.Contains("menu")){
print ("I have just clicked a part of the menu!");
}
print (hitCollider.name);
}
else{
print ("not detected any colliders :(");
}
}
The result in the first script is that it prints the name of the clicked collider whilst in the other script the result is that it prints "not detected any collider".
Note: maybe it is important to know that these two scripts are in two different unity projects. And I didnt forget to add a 2d collider! :P
thank you for even reading my question. Any help is appreciated!
Don't know much about 2D. Could this be a Rigidbody requirement problem thing?
Nope, because in the project in which this stuff is working I dont have a Rigidbody assigned as well
Answer by W.Walter · Jan 17, 2015 at 12:48 AM
It may be to do with the Z positioning of the colliders. Make sure the colliders in both your project have the same z position just to check its not that. Also your code will not work with 3d colliders because your using the Phsics2D stuff in your script.
Both colliders are 2D and they have the same z position. There also isnt anything between the colliders and the camera
What about raycast layers. Check Project Settings>Physics for the collision matrix.
What do u exactly mean with this? In both projects all the available layers are set to true
I know wy it didnt work! It had to do with the Camera settings: I had to change this to Orthographic before it worked! I think it is weird though that Camera.main.ScreenToWorldPoint doesnt seem to work with the perspective camera settings. W.Walter, do you know how to achieve the same result with the perspective Projection setting?
I think I just mark your answer as fine so that it's clear that this question got a answer :P
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Edge Collider issue 1 Answer
why Raycast2D doesn't work properly? 0 Answers
OnTriggerExit2D Does not work 1 Answer