Working in Editor, but Bad_Access Error in Xcode on iOS Device

Can you guys help me figure out what’s up with this code? It’s a scene selection menu. I have an array of buttons. If you click a button, it uses the GetLevelNumber() to figure out which level number it is, and then uses ClickLevelButton() to load that level. I open to better ideas on how to do this, but right now in the editor, it works perfectly with no warnings or errors, no null exceptions. But, when I build to the device, it hangs up when I press a button, and Xcode gives a Bad_Access Error on the object that has this script assigned to it. Can any of you see what I’m missing?

Thanks in advance!

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;

public class UI_Manager : MonoBehaviour
{
    public DataControl dataScript;

    public GameObject[] levelButtons;
    public Text levelTextObject;
    public string levelString;
    static public int levelNumber;
    public Text loadLevelIndicator;
    public Text arrayNumberIndicator;

    void Start()
    {
        dataScript = GameObject.FindGameObjectWithTag("DataControl").GetComponent<DataControl>();
        Debug.Log ("Current Level Number is: " + levelNumber);
        dataScript.Load();

    }

    public void ClickLevelButton(GameObject button)
    {
        GetLevelNumber(button);
        Application.LoadLevel("Level" + levelString);
    }

    public void GetLevelNumber(GameObject button)
    {
        levelTextObject = button.GetComponent<Text>();
        levelString = levelTextObject.text;
        bool isSuccess = int.TryParse(levelString, out levelNumber);
        if(isSuccess)
        {
            loadLevelIndicator.text = "Load Level: " + levelNumber;
            arrayNumberIndicator.text = "level Buttons[" + (levelNumber - 1) + "]";
        }
        else
        {
            loadLevelIndicator.text = "Can't Get Number!";
        }
    }

    public void LoadMenu()
    {
        if(dataScript.firstPlay)
        {
            //play movie

            //dataScript.firstPlay = false;
            //dataScript.Save();
            Handheld.PlayFullScreenMovie("Intro.mp4", Color.white, FullScreenMovieControlMode.Hidden, FullScreenMovieScalingMode.AspectFit);
            Application.LoadLevel("LevelSelect01");
        }
        else
        {
            Debug.Log ("NOT FIRST PLAY: Loading Level Selection Screen...");
            Application.LoadLevel("LevelSelect01");
        }
    }
}

Step through the debugger in Xcode to identify the line that throws EXC_BAD_ACCESS. Unity tends to be more forgiving. For example, this doesn’t raise any errors in Unity:

bool ToBool(object o) {
    try {
        return (bool) o;
    } catch {
        return false;
    }
}

I’m not saying the code above is the right way to code a typecast to bool, but the point is that it doesn’t throw errors. However, Xcode will give EXC_BAD_ACCESS on line 3 instead of letting try…catch catch it.

In your code, it’s probably a null reference that Unity handles more gracefully than Xcode, or an issue in int.TryParse().

1 Like

That was it. The int.TryParse() wasn’t liked on my device. I have rewritten my code to just use int’s from the beginning, and it works now. Thanks!

Happy to help!