05/9/18

Which came first; Awake(), OnEnable(), or Start()?

All Unity developers know that Awake() happens before OnEnable(), and both get called before Start(). Just to make sure, we visit the documentation to help us sleep at night. We never bothered to test it and simply assumed that all Awake() gets called before all OnEnable() before all Start(). But let’s say, out of boredom, we did test it.

Imagine 2 scripts, ScriptA and ScriptB, that do exactly the same thing: print out their game object’s name, script name, and the function being called (Awake(), OnEnable(), or Start()). Let’s have 2 game objects; Obj1, having both scripts, and Obj2, with just ScriptB. We run in the editor and see the logs to confirm what we already know.

Awake(), OnEnable(), and Start()

Surprise! Awake() can get called after OnEnable(). This may not look like a big deal until you start debugging with the wrong assumptions.

Let’s have another example; a single game object with a Rigidbody and the following 2 behaviors.

With the assumption that all Awake() gets called before all OnEnable(), then there should be no error. However, on the slight misfortune that ScriptD gets executed before ScriptC, ScriptD::OnEnable() will call ScriptC::Jump() before ScriptC gets to keep a reference to its Rigidbody, via ScriptC::Awake(). This results to a confusing Null Reference Exception that could haunt you for hours.

What the documentation doesn’t say is that Awake() is called before OnEnable() per behavior. All Start() occur after all Awake()/OnEnable(). We cannot determine which script gets executed first unless you manually specify that in the Script Execution Order Settings.

With that in mind, I pray to the Unity Gods that your bugs be all gone and your games be fun.

For those of you who are too lazy to type, feel free to download the source codes below.

ExecutionOrderTest
ExecutionOrderTest
ExecutionOrderTest.unitypackage
3.2 KiB
1006 Downloads
Details

P.S. yes, you can color your Debug Log messages 😀

01/23/17

Experiment at the GGJ17

My biggest takeaway in my GGJ17 experience was implementing a ‘sprite sheet animation‘-selector based on a given angle. This is only applicable to 2D games with more than 2 views per animation. If you haven’t read about our game from my last post, you can get more info here.

Here are the requirements:

  • Surfers could go be going to the right, down right, down, down left, or left
  • Dolphins could go left, up left, up, up right, right
  • Tiki could point left, up left, up, up right, and right

Each of these directions is a sprite sheet/sequence animation. The solution:

  • Each direction is a game object that animates through the sprite sheet/sequence
  • These direction game objects are children of a selector game object
  • The criteria of the selector is based on an absolute direction (to a target object, mouse, etc)
  • The selector activates the direction closest to the criteria and deactivates all others

If the directions weren’t animations (like the Tiki pointing), then a simple sprite selector based on angle would be enough.

A public repo of the project can also be found here. Here’s a UnityPackage of the demo:

AnimationAngleSelector
AnimationAngleSelector » Post
AnimationAngleSelector.unitypackage
1.5 MiB
1350 Downloads
Details

03/10/14

Games made by teens!

Another batch of Video Game Development Elective students from Philippine Science High School Main Campus will graduate this end of March. As a final requirement, they need to upload their games in Kongregate and here they are. Click on the image to play their games.

Change

Change was suppose to be a Chemistry-based game that ended up being a Pretentious Game clone. Go get emo with this one 😀

Cornered

Try to stay alive in this somewhat thriller survival game against creepy polygons without getting Cornered

Knockback

This is a difficult physics-based puzzle game using simple 3D objects. I hope your patience doesn’t easily thin out with Knockback

Rolling Rumble

Rolling Rumble was the most popular game during the National Science Fair exhibit. Go and push your friends around. I know you want to.

Synchonicity Alpha

Synchonicity Alpha will annoy you. Enough said.

Spectrum

Need some brain exercise? Spectrum will make your head twist and turn.

Sparkshot

Sparkshot is a tower-defense game using electricity and well… towers 😀

AIDS Infector

This is the most controversial game ever! Get down with the sickness with AIDS Infector

Balloony

And what’s a game exhibit without a Flappy Bird clone. Go get some candies and don’t let the Balloony pop

Logic Invaders

Who ever said binary ain’t fun? They obviously haven’t played Logic Invaders

Shooter

How long can you last in this fancy survival game – Shooter

I hope you guys have the patience of figuring out the controls of their games 😀 I will emphasize on integrating tutorial levels and making the controls more intuitive in my next game development classes.

Oh and feel free to rate and comment their games in Kongregate. I told them to expect the onslaught of the world once their games are out.

12/31/13

Touch Events in Unity3D

I’ve been working on Crazy Bugz for the past few days to take advantage of the 2D physics and sprites brought in by the latest version of Unity3D. Many things have been updated which reserves a post all by itself. For now, I want to discuss a discovery I made regarding the touch events. I’m not sure if this is iOS or Unity3D specific but I’ve built a work around that seems to be working for now 😀

For a demonstration, here’s a Unity3D package (requires 4.3). Build and test it on touch devices. I’ve only tested it for iOS devices. You can test it with Unity Remote but it has limitations like touch responsiveness which is critical for this demonstration.

TouchDemo
TouchDemo
TouchDemo.unitypackage
14.3 KiB
1879 Downloads
Details

First and foremost, I made a generic event handler for the different touch phases in TouchMonoBehavior.cs

The OnTouch* event handlers are all declared public virtual void and passes the Touch parameter. Meaning, each event handler could be called multiple times per Update() depending on how many touches there are (Input.touchCount). We can treat each touch separately by taking note of the finger id that comes with the touch.

All the while, I thought the touch phases have the following state diagram

Touch Phases State Diagram

Unfortunately, upon testing over and over again, it is POSSIBLE to start with Moved or Ended! I haven’t noticed if it could start with Stationary since I don’t use it for my projects at the moment. But my point here is that the Began phase CAN BE SKIPPED! I’m not sure if this is intentional but it’s happening and it got me pulling my hair for the past couple of days.

I’m using an object pool in my project where the objects react to touch. When the user touches, an object is created, let’s call that Object 0. When the user touches again, another object is created called Object 1. If Object 0 gets disabled (as part of the game mechanic) and the user touches again, Object 0 will be re-intialized and treated as something new. Objects don’t get destroyed, rather, they become disabled. This is basically how object pooling works.

Theoretically, every time a Began phase is encountered, a mirror is re-initialized in Crazy Bugz. The user can rotate or stretch this mirror by moving their finger which corresponds to the Moved phase. When the user releases their finger, an Ended phase is encountered and that mirror remains enabled until it gets disabled (shattered) by the laser. However, there are certain occasions where I touch and get a NullExceptionError. It turns out, my game is trying to look for a mirror with a specified finger id that was not created. This means, the Began phase was skipped!

As a work around, if in case the Began phase has been skipped and goes directly to Moved phase, I would treat that as a Began phase. In the case the Began phase has been skipped and goes directly to Ended, I would simply ignore it.

Not exactly the best solution but this will have to do.

The ideal case? Well, Began phase shouldn’t be skipped… ever 🙂