Events/delegates not working on Android (working in Unity player though)

Hello all,

following behaviour is driving me nuts. When using events/delegates the game works fine in the Unity player but they do not work when the game is deployed to an Android device (OS is 4.4 KitKat, behaviour/bug could also be reproduced on earlier Android versions).

The events are triggered by the player class and received by the UI class - the player eats a cookie and the UI shows the number of cookies eaten.

Player class excerpt:

using UnityEngine;
using System.Collections;

public class playerController : MonoBehaviour {
	//event handling
	public delegate void AddCookieAction();
	public static event AddCookieAction onAddCookie;

[...]

	// Update is called once per frame
	void Update () {
[...]
		Debug.Log ("ADD COOKIE FIRED");

		//send the add cookie event to the UI
		if (onAddCookie != null)
			onAddCookie();
[...]

UI class excerpt:

using UnityEngine;
using System.Collections;

public class UIController : MonoBehaviour {
	private int gvCookies;
	public GUIText gtCookies;
[...]
	void OnEnable(){
		//add to cookie score 
		playerController.onAddCookie += addCookie;
	}
[...]
	void addCookie (){

		gvCookies += 1;

		gtCookies.text = "Cookies: " + gvCookies;

		Debug.Log ("ADD COOKIE RECEIVED");

	}

When debugging the Android phone (via adb) following logcat is generated:

I/Unity   (15686):
I/Unity   (15686): (Filename:  Line: -1)
I/Unity   (15686):
I/Unity   (15686): ADD COOKIE FIRED
I/Unity   (15686):
I/Unity   (15686): (Filename: ./artifacts/AndroidManagedGenerated/UnityEngineDeb
ug.cpp Line: 53)
I/Unity   (15686):
I/Unity   (15686): NullReferenceException: Object reference not set to an instan
ce of an object
I/Unity   (15686):   at UIController.addCookie () [0x00000] in <filename unknown
>:0
I/Unity   (15686):   at (wrapper delegate-invoke) playerController/AddCookieActi
on:invoke_void__this__ ()
I/Unity   (15686):   at playerController.Update () [0x00000] in <filename unknow
n>:0
I/Unity   (15686):
I/Unity   (15686): (Filename:  Line: -1)
I/Unity   (15686):

Does anyone have an idea why this behaviour/bug occurs on the mobile device but not the Unity player?

Can someone help me out resolving this issue?

Many thanks in advance and kindest regards;
JunaOne

Well, the important thing we can learn here is that before you start debugging you should learn how to read an error message and a stacktrace. If you look at this:

NullReferenceException: Object reference not set to an instance of an object
at UIController.addCookie () [0x00000] in <filename unknown>:0
at (wrapper delegate-invoke) playerController/AddCookieAction:invoke_void__this__ ()
at playerController.Update () [0x00000] in <filename unknown>:0

You can see that you have a Nullreference exception. The callstack goest from bottom to top. It goes like this:

  • On your playerController class Update is being called by Unity →
  • Inside of Update you invoke a delegate wrapper. It’s actually a compiler generated delegate type. The actual method is “playerController/AddCookieAction:invoke_void__this__ ()” however that doesn’t really matter. It actually wraps the call to the “addCookie” method.
  • And here we have the last / topmost item in our stacktrace: UIController.addCookie. If that item shows up at the top of the stack trace that means the exception was thrown inside this method. If the delegate would be null there would be nothing about addCookie in the stacktrace.

That means we search for something inside the addCookie method that could throw a null reference exception. Luckily there’s only one thing inside that method that actually use a reference: “gtCookies”. So for some reason your reference to your GUIText is null / not setup. This has nothing to do with the delegate at all.

ps: Just in case you may think: “Hey but i have a Debug.Log in addCookie but it doesn’t show up”. Sure, there is a Debug.Log below the line that causes an exception. An exception terminates the execution of the current execution. Usually an unhandled exception would actually completely terminate your whole program. However, Unity handles exceptions thrown in it’s callbacks. So the exception travels up the callstack until Unity catches the exception at the point where it called Update for you.

So the exception thrown inside the addCookie method will terminate addCookie, and terminate the Update method of playerController right at the point where you call your delegate.

I think I fixed my issue. No guarantees but it looks like the same problem. Whenever you raise an event, make sure that the event member variable is not null. Just do a normal if (event != null) sendevent(); and that stopped the crashes I had.