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:
NullReferenceException: Object reference not set to an instance of an object
Movimiento2.Update ()
Can I ignore it?
Thanks in advance.