The Unite 2014 keynote was yesterday. There’s lots of exciting stuff coming for Unity (I’ll cover that in another post), but you can watch for yourself:
Well, he just started an IndieGoGo campaign to fund a space in the Netherlands called “Kenney Land,” for game developers to work, learn, and play. The campaign closes on 18 October, so you should check it out while you have the chance! If that idea alone is not reason enough to throw your money at him, backers at the $20+ level get a brand new 2D asset pack, including reworks and expansions for some of his previous work, a 2100+ asset “Roguelike pack,” and a bunch of audio clips and tracks.
What are you waiting for? Go!
— Derek Stobbe (@djStobbe) August 19, 2014
Here’s an example of the default script that Unity will create when you ask for a new C# script:
Most of this makes sense, but I’ve found myself wondering, “why would they include
System.Collections, but not
System.Collections.Generic?” There aren’t very many cases where I’d rather use an array than a generic List. The other default import,
UnityEngine, makes sense because it provides access to the Unity API, but I couldn’t figure out
I discovered the answer while reading through Catlike Coding‘s Unity tutorials (specifically, the tutorial on making fractals):
Move the two child creation lines to a new method named
CreateChildren. This method needs to have
IEnumeratoras a return type, which exists in the
System.Collectionsnamespace. That’s why Unity includes it in their default script template and why I included it in the beginning as well.
- Catlike Coding, Constructing a Fractal
CreateChildren in the script referenced is a coroutine[ref]See the Unity manual entry on Coroutines for more information[/ref], which provides our answer: Unity coroutines must have a return type of
IEnumerator, which is found in the
System.Collections namespace. As a core framework feature, it makes sense that the Unity team would choose to include this by default in the new scripts created by the editor.
I wrote this script to have a GameObject follow the mouse cursor, converting mouse position to whatever coordinate space you need. I use it with viewport space to display grid coordinates next to the cursor with a GUIText component.
public class MouseFollow : MonoBehaviour
new public Camera camera;
public float zValue = 0;
public enum CoordinateSystem
public CoordinateSystem convertTo;
new Transform transform;
transform = GetComponent<Transform>();
if (camera == null)
camera = Camera.main;
Vector3 converted = Input.mousePosition;
if (convertTo == CoordinateSystem.World)
converted = camera.ScreenToWorldPoint(converted);
} else if (convertTo == CoordinateSystem.Screen)
// Input.mousePosition is already in screen space
} else if (convertTo == CoordinateSystem.Viewport)
converted = camera.ScreenToViewportPoint(converted);
converted.z = zValue;
transform.position = converted;