- Home /
Delegates not working on iOS
Hi Unity Peeps,
So I have the following code that works fine in the Editor but not on iOS and was wondering if someone could tell me why and point me towards a possible solution. Basically the MyCallBack function is not getting called on an iOS device but does in the editor.
public class MyClassOne : MonoBehaviour {
MyClassTwo myClassTwo;
void Start ()
{
myClassTwo = new MyClassTwo();
MyFunction();
}
void MyFunction ()
{
myClassTwo.MyFunctionInClassTwo(MyCallBack);
}
void MyCallBack (bool isSuccessful)
{
if(isSuccessful)
{
Debug.Log(":)");
}
else
{
Debug.Log(":(");
}
}
}
public class MyClassTwo : MonoBehaviour {
public delegate void MyDelegate(bool isSuccessful);
private MyDelegate myDele;
public void MyFunctionInClassTwo (MyDelegate functionToCall)
{
myDele = functionToCall;
MySecondFunctionInClassTwo();
}
void MySecondFunctionInClassTwo ()
{
myDele(true);
}
}
Hmm.I don't have an IOS device but i can certainly spot something wrong here.You should never create a new object which inherits from $$anonymous$$onoBehavior.Ins$$anonymous$$d you have to get it's component.$$anonymous$$aybe you can try this:
public class $$anonymous$$yClassOne : $$anonymous$$onoBehaviour
{
void Start()
{
gameObject.GetComponent<$$anonymous$$yClassTwo>().$$anonymous$$yFunctionInClassTwo($$anonymous$$yCallBack);
}
void $$anonymous$$yCallBack(bool isSuccessful)
{
if (isSuccessful)
{
Debug.Log(":)");
}
else
{
Debug.Log(":(");
}
}
}
public class $$anonymous$$yClassTwo : $$anonymous$$onoBehaviour
{
public delegate void $$anonymous$$yDelegate(bool isSuccessful);
private $$anonymous$$yDelegate myDelegate;
public void $$anonymous$$yFunctionInClassTwo($$anonymous$$yDelegate functionToCall)
{
myDelegate = functionToCall;
myDelegate(false);
}
}
Place the two classes separately in one game object and maybe you can post back if it works.
thanks, i am actually doing the equivalent of that in my real code, this was just something i bashed out to show my situation, in reality the two classes are on separate objects and there both singletons.