Search Unity

Problems with C# interfaces on android

Discussion in 'Scripting' started by Joseph_d, Jul 3, 2015.

  1. Joseph_d

    Joseph_d

    Joined:
    May 20, 2013
    Posts:
    3
    Hello everyone,

    I am new to mobile development and I have encountered a strange problem. I have created a simple message system, which works great when I run it in the editor. However when I run it on my android device, none of the methods get called that should be called by my message system that uses interfaces. Does anyone have any idea what may be happening.

    Please let me know if you need any more information.

    Thank you
    Joseph.
     
  2. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    We need more information.
     
  3. Joseph_d

    Joseph_d

    Joined:
    May 20, 2013
    Posts:
    3
    Alright, I guessing you want the code that is being problematic.

    Here is the EventManager Class
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public interface IListner
    6. {
    7.      void OnEventOccurred(EventManager.EventTypes eventName);
    8. }
    9. public class EventManager : MonoBehaviour
    10. {
    11.  
    12.      public enum EventTypes { PlayerDeath, PlayerLeftGround };
    13.  
    14.      private Dictionary<EventTypes, List<IListner>> eventList = new Dictionary<EventTypes,List<IListner>>();
    15.  
    16.      public void AddEventListner(IListner listener, EventTypes eventName)
    17.      {
    18.           if(eventList.ContainsKey(eventName) == true)
    19.           {
    20.                eventList[eventName].Add(listener);
    21.           }
    22.           else
    23.           {
    24.                eventList.Add(eventName, new List<IListner>());
    25.                eventList[eventName].Add(listener);
    26.           }
    27.      }
    28.  
    29.      public void CallEvent(EventTypes eventName)
    30.      {
    31.           if(eventList.ContainsKey(eventName) == false)
    32.           {
    33.                return;
    34.           }
    35.           foreach(IListner eventListner in eventList[eventName])
    36.           {
    37.                eventListner.OnEventOccurred(eventName);
    38.           }
    39.      }
    40. }
    It uses interfaces to allow other classes to broadcast messages. Another class that has registered as a listener will the run the appropriate function upon receiving the message.

    Here is a piece of one of classes that sends a message.

    Code (CSharp):
    1.      public void OnCollisionEnter2D(Collision2D collision)
    2.      {
    3.           if (collision.gameObject.tag == "Platform")
    4.           {
    5.                ResetJumpCounter();
    6.                Speed = Speed / 2;
    7.           }
    8.           if(collision.gameObject.tag == "Ground")
    9.           {
    10.                if (collision.gameObject.GetComponent<Ground>().GetDoesGroundKillPlayer() == true)
    11.                {
    12.                     GameManager.Events.CallEvent(EventManager.EventTypes.PlayerDeath);
    13.                }
    14.                ResetJumpCounter();
    15.                Speed = Speed / 2;
    16.           }
    17.      }
    Here is a class that receives the message and then acts upon that information.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUIManager : MonoBehaviour, IListner
    5. {
    6.      public Canvas ScoreCanvas = null;
    7.      // Use this for initialization
    8.      void Start()
    9.      {
    10.           if(ScoreCanvas != null)
    11.           {
    12.                ScoreCanvas.enabled = false;
    13.           }
    14.           GameManager.Events.AddEventListner(this, EventManager.EventTypes.PlayerDeath);
    15.      }
    16.  
    17.      // Update is called once per frame
    18.      void Update()
    19.      {
    20.  
    21.      }
    22.  
    23.      public void OnEventOccurred(EventManager.EventTypes typeOfEvent)
    24.      {
    25.           ScoreCanvas.enabled = true;
    26.      }
    27. }
    My problem is that my message system works great when I run it in the editor but when I run it on my android phone it does not work. It never seems to call any of the functions it is supposed to.

    Thanks for any advice or help.

    Joseph
     
  4. Joseph_d

    Joseph_d

    Joined:
    May 20, 2013
    Posts:
    3
    So it turns out the problem was not interfaces, it was actually a null reference. I had added a required component to my gamemanger class and then forgot to delete it from the scene and add it again. So it was never getting a event system assigned. If anyone else runs across random behavior when running on android be sure to check logcat to see what is actually happening on the device.
     
    eisenpony likes this.
  5. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    Too bad you didn't get any help but thanks for the tip!