I needed a circular range control to be for a project using Unity UI, and there seemed not to be one built-in, I created one. I wanted it to work on both desktop and mobile (touch). In building it I found out that to make it usable it requires quite a bit of tuning. [Read more…]
Untangling GameObject State in Unity
In this short post and video I try to discuss and clarify a few points about GameObject state in Unity with respect to game, scene and “runtime” scope. It is a bit more complex than one may understand initially, so bear with me a little.
When you start developing scenes in Unity, it won’t take long before you start asking questions like:
How can I get the same GameObject in different scenes?
Why do static properties sometimes get reset across scenes?
Why when I reload a scene I get duplicated objects which are meant to be singletons?
How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?
Here are some answers.
Here is the full schema I refer to:
While MonoBehaviour’s life-cycle is quite well documented e.g. both directly in Unity docs here and also by third parties e.g. here, how to handle GameObject’s persistence in and across scenes may be more obscure.
So here are written (partial) answers to the questions above:
How can I get the same GameObject in different scenes?
Would be probably better to reword this as “how can I persist and share data across scenes” – and there are many ways to do that
Why do static properties sometimes get reset across scenes?
Only static properties which are GameObjects present in the scene will get reset (typically singletons). Other static properties will be preserved across the virtual machine.
Why when I reload a scene I get duplicated objects which are meant to be singletons?
That is because you marked those objects with DontDestroyOnLoad and created them (also) in other scenes. Create them via code (not in hierarchy) checking before their existence.
How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?
This is best done just as explained above: create the global objects in every scene via code if they don’t already exist.
If you are in need of learning some good patterns in software game development, a really nice book is Game Programming Patterns. Here are also 50 Tips for Working with Unity (Best Practices) which I reread from time to time, understanding progressively more and more of them (but still not all :D).
Thanks to Daniele Giardini for some feedback on the state scheme above.
Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied / serious games.
Unity3d: Using A* and grids for entering a football field
Just played with the A* algorithm and a grid to make players enter a field and assume the initial 4-4-2 classical formation. Notice there are dumb and smart teams [Read more…]
Game Development with Unity 2D – part 6: Pixel Perfect
Here we have a nice chat with the sinful Daniele Giardini about pixel perfect in 2D games, as a form of holy perfection.
We are continuing our voyage in Unity 2D development; previous trips: Part 0, Part 1, Part 2, Part 3, Part 4, Part 5.
We didn’t find a satisfactory definition of pixel perfect online, so this is Daniele’s: [Read more…]
A very short note on Unity 2D and pixel characters
Just a note on a simple problem with Unity 2D and small sprites’ physics.
Problem
I could not get my 2 pixel by 2 activist demonstrating in the scene to collide with buildings when working in 2D. By using the 3D box collider and locking rotations everything worked fine, but it was not cool at all given the very flat design of People in Love.
I am using 2D Toolkit for handling sprites and atlases, and I was using its built-in 2D physics collider integration, letting it compute the perfectly fitting box collider. But I didn’t get (2D) collisions.
But then using Unity 4.5.2 I got this warning at runtime: “The collider did not create a collision shape…”
Turns out this is the problem:
Box Collider 2D does not collide when its size is small (like 0.01)
Solution
The solution is not hard: I simply set in 2D Toolkit to leave the colliders as user defined, and I added an Edge Collider 2D to the activist prefab. Works fine
EDIT: Thanks to a suggestion from the ineffable Daniele Giardini I’m now using a Circle Collider 2D which works fine even at 0.003 radius and fits perfectly with my activists.
Now I am a Unity beginner dev so any suggestion on refining this would be very welcome – I’m here on Twitter.
Game Development with Unity 2D – part 5: A grid of Roman centuriae
This is the fifth part of a voyage in Unity 2D development, where we document in video the design and development of a simple strategic game reproducing the Battle of the Teutoburg Forest; here we make a more general discussion of grids in games. If you missed them here are Part 0, Part 1, Part 2, Part 3, Part 4.
We’ll dedicate this episode to discussing grids, which are at the core of so many 2D (and 3D) strategy games. Up to now our battle has been in continuous space, but it would be cool to have a grid so we can more easily create an AI with a strategy. [Read more…]
Game Development with Unity 2D – part 4: Oplites, Back to Basics!
Game Development with Unity 2D – part 3: GUI, or No Text is an Island
This is the third part of a voyage in Unity 2D development, where we document in video the design and development of a simple strategic game reproducing the Battle of the Teutoburg Forest. Here are Part 1, Part 2, Part 4.
Video
In this part three we are going to have health bars, labels and winning conditions; here is the video (also directly on YouTube watch it in HD for a better experience): [Read more…]
Game Development with Unity 2D – part 2: Javelins & Battle Cries
Game Development with Unity 2D – part 1: The Battle of the Teutoburg Forest
In this episode of our voyage in Unity 2D development, we create the first version of our Battle of the Teutoburg Forest.
Here are Part 0, Part 2, Part 3, Part 4, Part 5.
In this first video we see the basics of Unity 2D sprites and animations (as provided in Unity 4.3 and following), and a base class structure for a strategic game. [Read more…]