Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by AdamCNorton, Nov 25, 2014.

  1. AdamCNorton

    AdamCNorton

    Joined:
    Apr 16, 2013
    Posts:
    57
    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!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System;
    4. using System.Collections;
    5.  
    6. public class UI_Manager : MonoBehaviour
    7. {
    8.     public DataControl dataScript;
    9.  
    10.     public GameObject[] levelButtons;
    11.     public Text levelTextObject;
    12.     public string levelString;
    13.     static public int levelNumber;
    14.     public Text loadLevelIndicator;
    15.     public Text arrayNumberIndicator;
    16.  
    17.     void Start()
    18.     {
    19.         dataScript = GameObject.FindGameObjectWithTag("DataControl").GetComponent<DataControl>();
    20.         Debug.Log ("Current Level Number is: " + levelNumber);
    21.         dataScript.Load();
    22.  
    23.     }
    24.  
    25.     public void ClickLevelButton(GameObject button)
    26.     {
    27.         GetLevelNumber(button);
    28.         Application.LoadLevel("Level" + levelString);
    29.     }
    30.  
    31.     public void GetLevelNumber(GameObject button)
    32.     {
    33.         levelTextObject = button.GetComponent<Text>();
    34.         levelString = levelTextObject.text;
    35.         bool isSuccess = int.TryParse(levelString, out levelNumber);
    36.         if(isSuccess)
    37.         {
    38.             loadLevelIndicator.text = "Load Level: " + levelNumber;
    39.             arrayNumberIndicator.text = "level Buttons[" + (levelNumber - 1) + "]";
    40.         }
    41.         else
    42.         {
    43.             loadLevelIndicator.text = "Can't Get Number!";
    44.         }
    45.     }
    46.  
    47.     public void LoadMenu()
    48.     {
    49.         if(dataScript.firstPlay)
    50.         {
    51.             //play movie
    52.  
    53.             //dataScript.firstPlay = false;
    54.             //dataScript.Save();
    55.             Handheld.PlayFullScreenMovie("Intro.mp4", Color.white, FullScreenMovieControlMode.Hidden, FullScreenMovieScalingMode.AspectFit);
    56.             Application.LoadLevel("LevelSelect01");
    57.         }
    58.         else
    59.         {
    60.             Debug.Log ("NOT FIRST PLAY: Loading Level Selection Screen...");
    61.             Application.LoadLevel("LevelSelect01");
    62.         }
    63.     }
    64. }
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    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:
    Code (csharp):
    1. bool ToBool(object o) {
    2.     try {
    3.         return (bool) o;
    4.     } catch {
    5.         return false;
    6.     }
    7. }
    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().
     
    AdamCNorton likes this.
  3. AdamCNorton

    AdamCNorton

    Joined:
    Apr 16, 2013
    Posts:
    57

    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!
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Happy to help!