C# Events and iPhone causes crash

Does anyone have success using c# events on the iPhone?

I have compiled this simple example that shows the problem. The example runs fine in the Editor but when executed on the iPhone it will crash.

I recall some limitations with the current iPhone implementation from Unite 2008 lecture regarding the iPhone, but I can’t remeber if Events was one of them.

using UnityEngine;
using System.Collections;

public class TestEvents
{
    // Setup of the Test Event
    public delegate void TestEventHandler();
    public event TestEventHandler TestEvent;
    public virtual void OnTestEvent()
    {
        if (TestEvent != null) TestEvent();
    }

    // Test function to see if the event is working.
    public void TriggerEvent()
    {
        OnTestEvent();
    }
}

public class EventsBreakOnIPhone : MonoBehaviour
{

    TestEvents testEvent;
    public void HandleTestEvent()
    {
        // This will be spammed when running in the editor
        Debug.Log("Test Event Handled");
    }
    void Start()
    {
        testEvent = new TestEvents();

        // Register as a subscriber to the TestEvent: This breaks when executed on the iPhone but works fine in the editor
        testEvent.TestEvent += new TestEvents.TestEventHandler(HandleTestEvent);
    }

    void Update()
    {
        // Trigger the event through TriggerEvent and wait for 'HandleTestEvent' to write in the log
        testEvent.TriggerEvent();
    }
}

100176–3872–$eventsbreakoniphone_630.cs (1.09 KB)

I filed a bug report showing the problem, so hopefully a work-around or a fix will be made.
I will post any progress on the case here.

Only 3 things I could think of.

  • Remove the ‘virtual’ keyword. Or even remove that function all together and just call TestEvent(); from TriggerEvent directly.

  • Call testEvent.TriggerEvent(); in Start instead of Update

  • Get rid of the “if (TestEvent != null)” test.