Notation
(This is only here for personal reference, since this is rather basic programming …)
/// <summary> /// Formats the input string (in seconds) as a human readable /// string in the form of MM:SS. /// </summary> /// <returns> /// A <see cref="System.String"/> of the current playing time. /// </returns> private string formatedTimeString (string input) { int seconds; int minutes; minutes = Mathf.FloorToInt(input / 60); seconds = Mathf.FloorToInt(input - minutes * 60); string r = (minutes < 10) ? "0" + minutes.ToString() : minutes.ToString(); r += ":"; r += (seconds < 10) ? "0" + seconds.ToString() : seconds.ToString(); return r; }
When preparing my game for Fantoche, I ran into problems several times, since upon building my game, the screen either remained black or parts of my game did not work, with the log showing a NullReferenceException.
In both times, it was related to me tagging objects in the Unity 3D editor. This is caused by the somewhat peculiar implementation of tagging in Unity which only allows one tag to be added to an object.
Pitfall No. 1: EditorOnly
Tagging objects in the editor as EditorOnly will make the game work in the editor – but not in the build, as those objects won't be included when building your game.
Pitfall No. 2: Trying to Access One of Several Objects with the Same Tag
When accessing GameObjects using the FindObjectsWithTag() function, make sure that only one object has this tag added.
The function returns the first object with this tag it encounters.
How not to do it:
using System; using UnityEngine; using System.Collections; using System.Collections.Generic; /// <summary> /// Needs work. /// </summary> public class Inventory : MonoBehaviour { private Dictionary<string, int> backpack; private bool showInventory; void Awake () { } void Start () { DisplayInventory (true); } public void DisplayInventory (bool state) { showInventory = state; } public bool RemoveObject (string iObject, int number) { try { backpack[iObject] = backpack[iObject] - number; if (backpack[iObject] <= 0) { backpack.Remove (iObject); } return true; } catch (Exception ex) { print ("[" + this.GetType ().ToString () + "] The object " + iObject + " does not exist."); return false; } } public bool AddObject (string iObject, int number) { if (backpack.ContainsKey (iObject)) { backpack[iObject] += number; } else { backpack[iObject] = number; } return true; } void OnGUI () { if (showInventory) { int counter = 0; foreach (KeyValuePair<string, int> kv in backpack) { string displayString = kv.Key + ": " + kv.Value.ToString (); GUI.Label (new Rect (10, counter * 40, 200, 30), displayString); counter++; } } } }
How to do it instead:
using System; using UnityEngine; using System.Collections; public class ChickenCounter : MonoBehaviour { private int counter = 0; public GameObject particleEffect; void OnTriggerEnter (Collider hit) { if (hit.tag == "bonus") { counter++; Instantiate (particleEffect, hit.transform.position, hit.transform.rotation); Destroy (hit.gameObject); } } void OnGUI () { GUI.Label(new Rect(10,10,200,30),