- Home /
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
Did you ever manage to resolve this issue? I am experiencing exactly the same problem - an app that works fine in iOS / Webplayer - switched to Android and all delegates are broken...
I have a similar problem, but am only using iOS devices now. Too bad there are hardly ever any answers around here??
I have the exact same issue! This was the only thing I could find on Google, related to it D:
zoooom:
I
have not resolved this issue - since we abandoned the game idea and switched back to the classic approach of calling the class/object methods. Would like to see this issue resolved though since the event based approach is much cleaner.
$$anonymous$$r_FJ Please list the Unity Version (I have also forgotten to do this ;) ). With some luck we will attract someone with a fix for this issue. Having the version ready will definitely help.
@$$anonymous$$r_FJ: I'm pretty sure you don't have the exact same problem as this has nothing to do with delegates. If you want help on your specific problem, please don't post comments on other questions but post your own question. You might want to put a link to this one into your question if you think it's actually related.
Answer by Bunny83 · Dec 14, 2014 at 02:58 AM
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.
Thanks Bunny83!
Since we're not working on the game anymore I'm not able to reproduce the bug. But your analysis makes it absolutely clear that the unassigned variable must have been the problem.
Thanks again for taking the the time to answer this question and explaining the stack trace in detail.
@JunaOne: No problem ;) Unity Answers is here to help others with similar problems as well.
Answer by Umai · Dec 14, 2014 at 01:34 AM
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.