- Home /
Why 2D colliders doesn't work?
First of all apologies if there are any mistakes on my english, isn't my first language. I'm a very begginer in unity and I'm learning the basics right now, I'm creating a game where the main gameObject should move to another gameObject position on mouse right click. This is the code I have:
GameObject GetClickedGameObject()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
return hit.transform.gameObject;
else
return null;
}
void Update () {
GameObject clickedGmObj = null;
if(Input.GetMouseButtonDown(0)){
clickedGmObj = GetClickedGameObject();
if(clickedGmObj != null)
Debug.Log(clickedGmObj.name);
alien.transform.position = (clickedGmObj.transform.position);
}
}
Everything is fine when I use 3D colliders, the object (alien) moves to the position I expected, but my game is in 2D, so I would like to use 2D colliders also in order to modify their shape, because every collider delimits one territory. The thing is when I use 2D coll. nothing happens when clicked.
Another doubt I have is that when I click somewhere where there isn't an object, the game is paused and this message appears in the console:
Movimiento2.Update () Can I ignore it? Thanks in advance.NullReferenceException: Object reference not set to an instance of an object
You need to clear the air first . Few questions .
1) While Setting up project details , you've created a 3D game or 2D game ? 2) Alien Gameobject is what ? 3D gameobject or 2D gameobject ? .
Just for your information Collider has nothing to do here ,If game object is not transfor$$anonymous$$g to that position . Transform is default property of each Game Object not collider . You have to deal with the NullReferenceException first .
1)The project is set in 2D mode. 2)Alien GameObject is a 2D object, sprite type.
$$anonymous$$y problems were solved by the answer below, but thanks for your interest in helping.
Answer by EvilTak · Apr 29, 2014 at 02:13 PM
First of all, your code is wrong for 2D. Your GetClickedGameObject() function has code for 3D not 2D. That's why you are getting NullReferenceException because clickeGmobj is null and you are trying to get transform of null object. To avoid this, include
alien.transform.position = (clickedGmObj.transform.position);
In your if statement before that. For 2D, your GetClickedGameObject() function should be:
GameObject GetClickedGameObject()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero); //use Vector2.zero only when object will be directly under camera.
if (hit.collider)
return hit.transform.gameObject;
else
return null;
}
Problems solved. Lots of thanks evil tak, I didn't know the difference between 2D and 3D code in this case, forgive my ignorance. This is the C# script I have right now, and it works perfectly!
GameObject GetClickedGameObject()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero); //use Vector2.zero only when object will be directly under camera.
if (hit.collider)
return hit.transform.gameObject;
else
return null;
}
void Update () {
GameObject clickedGmObj;
if(Input.Get$$anonymous$$ouseButtonDown(0)){
clickedGmObj = GetClickedGameObject();
if(clickedGmObj != null) {
Debug.Log(clickedGmObj.name);
alien.transform.position = (clickedGmObj.transform.position);
}
clickedGmObj = null;
}
}