Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by JunaOne · May 07, 2014 at 02:58 PM · c#eventsdelegate

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

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zoooom · Sep 04, 2014 at 11:09 AM 0
Share

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...

avatar image Umai · Dec 12, 2014 at 06:15 AM 0
Share

I have a similar problem, but am only using iOS devices now. Too bad there are hardly ever any answers around here??

avatar image Mr_FJ · Dec 13, 2014 at 02:55 PM 0
Share

I have the exact same issue! This was the only thing I could find on Google, related to it D:

avatar image JunaOne · Dec 13, 2014 at 03:35 PM 0
Share
  • 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.

avatar image Bunny83 · Dec 14, 2014 at 03:08 AM 1
Share

@$$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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JunaOne · Dec 18, 2014 at 04:05 PM 1
Share

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.

avatar image Bunny83 · Dec 18, 2014 at 04:23 PM 0
Share

@JunaOne: No problem ;) Unity Answers is here to help others with similar problems as well.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Dynamic Anonymous Delegates/Lambda On UntyEvent 1 Answer

C# and JavaScript Event system causing 1 Answer

How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges