How Many Ds Are There in Unity?

I’ve started learning Unity 3D, which is my third game development engine in as many years.

I’ve always developed 2D games because I don’t have any 3D skills whatsoever. Well, I can walk about without bumping into stuff, but that’s about it. However, I read up on Unity and watched a few videos and it looked relatively¬†easy to get started with. I then realised that they had introduced a 2D development version – basically the 3D version, but with an ability to lock the game to a 2D view – and that really sealed the deal.

I spent a couple of hours this evening trying to get some basic 2D objects moving and colliding. There is a wealth of excellent learning content for Unity, both on the official site and on various other sites. After reading, watching and typing I managed to get a very simple Unity scene working. All apart from one bit; getting objects to fire collision events.

This code should have seen my collision event firing;

 

void OnCollisionEnter (Collision col)
{
   if (col.gameObject.name == "Enemy")
   {
      Destroy(col.gameObject);
   }
}

As expected with any new programming endeavour this did not work. At all. Not one bit of it.

And this is where I discovered just how many Ds there are in Unity.

Unity was originally a 3D engine – and still is, of course – and so all the original functions like OnCollisionEnter did not need “3D” appending to their names. But then they made a pesky 2D mode and all hell broke loose and it made me cry.

See, all the info I was reading was referencing the original 3D versions of the collision functions, and it was only after a lengthy search that I stumbled across the best Unity link ever as it said “Use OnCollisionEnter2D, you muppet!” and all was well.

So, for anyone else confused by this stuff, here is the code you actually need for trapping collisions in the 2D version of Unity.

void OnCollisionEnter2D (Collision2D col)
{
   if (col.gameObject.name == "Enemy")
   {
      Destroy(col.gameObject);
   }
}

And that is how I found out there are 2Ds in Unity.

Leave a Reply

%d bloggers like this: