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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity 5 Android game crashing when GUI is present

Discussion in 'UGUI & TextMesh Pro' started by SamuraiKitty, Nov 14, 2015.

  1. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    A strange problem is happening when I play my game on Android, and I'm nearly 100% sure that it is because of the GUI in the game. After I reload the level about 3 times (with a GUI button) the game suddenly crashes. If I turn the GUI off, compile and then play the game on Android, it doesn't crash when I reload (I set it so that the level automatically reloads when the player dies).

    I have only tested this on Android, not iOS, but my guess is that the same thing would happen.

    Here is the crash log.

    I believe it is how I'm setting up the GUI and how the code(C#) works with it. I'm simply setting the GUI's in the Canvas to active when I need them, and inactive when I don't. I'm not too sure if I should be doing this. I am a newbie at Unity and it's GUI system, but I have been developing in C# and C++ for a while. Please tell me if I'm doing this right or not. Should I instantiate the GUI from prefabs, or directly from code? Or is this the way I should do it?

    Since I have no idea what is going on with the GUI and how to fix this, I need your help. I will post every GUI related script I have on this thread. I have added little comments everywhere explaining everything.

    PlayerScript.cs - Mainly handles player movement, player death, and GameOver GUI.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerScript : MonoBehaviour {
    6.  
    7.     //Public variables
    8.     //Ints
    9.     public int gameOverY;
    10.     public int verticalJump;
    11.     public int rayHeight;
    12.     public int rotateA, rotateB;
    13.     public int gameOverScreenY;
    14.  
    15.     //Floats
    16.     public float rotationSpeed;
    17.     public float totalRotation = 0;
    18.     public float speed;
    19.     public float raz = 0;
    20.  
    21.     //Vector3s
    22.     public Vector3 jumpPower;
    23.  
    24.     //GameObjects
    25.     public GameObject ps;
    26.     public GameObject mainCam;
    27.  
    28.     //GUI
    29.  
    30.     //RectTransforms
    31.     public RectTransform gameOverButtons;
    32.     public RectTransform gameOverText;
    33.     public RectTransform scoreBoardUI;
    34.     public RectTransform leaderBoardUI;
    35.  
    36.     //Input Fields
    37.     public InputField name;
    38.  
    39.     //Buttons
    40.     public Button submitButton;
    41.  
    42.     //Texts
    43.     public Text inputPlaceHolder;
    44.     public Text scoreText;
    45.  
    46.     //Private variables
    47.     //Ints
    48.     private int score = 0;
    49.     private int highScore = 0;
    50.  
    51.     //Bools
    52.     private bool jumping;
    53.     private bool isDead;
    54.     private bool deadFalling;
    55.     private bool canJump;
    56.     private bool jump;
    57.     private bool rotateLeft;
    58.     private bool rotateRight;
    59.  
    60.     //Strings
    61.     string highScoreKey = "HighScore";
    62.  
    63.     //Scripts
    64.     private StartManager startManager = new StartManager();
    65.  
    66.     //Vector3s
    67.     private Vector3 dir;
    68.     private Vector3 tempDir;
    69.  
    70.     //Rigidbodies
    71.     private Rigidbody rb;
    72.  
    73.     //Transforms
    74.     private Transform child;
    75.  
    76.     //Quaternions
    77.     Quaternion a;
    78.     Quaternion b;
    79.  
    80.     // Use this for initialization
    81.     void Start ()
    82.     {
    83.         //Set variables
    84.         isDead = false;
    85.         canJump = false;
    86.         dir = Vector3.zero;
    87.         scoreText.text = "0";
    88.         jumping = false;
    89.  
    90.         //Set GameObject variables
    91.         GameObject tile = GameObject.FindWithTag("Tile");
    92.         rb = GetComponent<Rigidbody>();
    93.         GetComponent<Animator>().CrossFade("start", 0f);
    94.         child = transform.GetChild(0);
    95.     }
    96.  
    97.     // Update is called once per frame
    98.     void Update ()
    99.     {
    100.         //Raycasting
    101.         RaycastHit hit;
    102.         Ray downRay = new Ray(new Vector3(transform.position.x - 0.485f, transform.position.y, transform.position.z + 0.485f), -transform.up);
    103.         Debug.DrawRay(new Vector3(transform.position.x - 0.485f, transform.position.y, transform.position.z + 0.485f), -transform.up * rayHeight);
    104.  
    105.         //If the player is not dead
    106.         if (!isDead)
    107.         {
    108.             //Check if he is grounded and not jumping
    109.             if (Physics.Raycast(downRay, out hit, rayHeight) && !jumping)
    110.             {
    111.                 //Keyboard controls
    112.  
    113.                 //Movements on space bar down
    114.                 if (Input.GetKeyDown("space") && dir == Vector3.forward)
    115.                 {
    116.                     MoveLeft();
    117.                 }
    118.                 else if (Input.GetKeyDown("space") && dir == Vector3.left)
    119.                 {
    120.                     MoveRight();
    121.                 }
    122.                 else if (Input.GetKeyDown("space") && dir == Vector3.zero)
    123.                 {
    124.                     StartMoving();
    125.                 }
    126.  
    127.                 //If the player can jump, his Raycast is hitting the ground (grounded) and we press the up arrow, then jump
    128.                 if (Physics.Raycast(downRay, out hit, rayHeight) && Input.GetKeyDown(KeyCode.UpArrow) && canJump)
    129.                 {
    130.                     Jump();
    131.                 }
    132.  
    133.                 //Touch controls
    134.  
    135.                 //Get left touch (change direction) and right touch (jump)
    136.                 if (Input.touchCount > 0)
    137.                 {
    138.                     if (Input.GetTouch(0).phase == TouchPhase.Began)
    139.                     {
    140.                         Touch touch = Input.GetTouch(0);
    141.                         if (touch.position.x < Screen.width / 2)
    142.                         {
    143.                             if (dir == Vector3.forward)
    144.                             {
    145.                             MoveLeft();
    146.                             }
    147.                             else if (dir == Vector3.left)
    148.                             {
    149.                                 MoveRight();
    150.                             }
    151.                             else if (dir == Vector3.zero)
    152.                             {
    153.                                 StartMoving();
    154.                             }
    155.                         }
    156.                         else if (touch.position.x > Screen.width / 2)
    157.                         {
    158.                             if (Physics.Raycast(downRay, out hit, rayHeight) && canJump)
    159.                             {
    160.                                 Jump();
    161.                             }
    162.                         }
    163.                     }
    164.                 }
    165.                 //Get touch swipes for jumping and changing direction at same time
    166.                 Touch[] touchSwipe = Input.touches;
    167.                 for (int i = 0; i < Input.touchCount; i++)
    168.                 {
    169.                     raz = touchSwipe[i].deltaPosition.x;
    170.                     if (touchSwipe[i].phase == TouchPhase.Moved)
    171.                     {
    172.                         if (raz > 0) //if swipe left
    173.                         {
    174.                             //Jump and go left
    175.                             jump = true;
    176.                             MoveLeft();
    177.                         }
    178.                         if (raz < 0) //if swipe right
    179.                         {
    180.                             //Jump and go right
    181.                             jump = true;
    182.                             MoveRight();
    183.                         }
    184.                     }
    185.                 }
    186.             }
    187.  
    188.             else if (jumping == true)
    189.             {
    190.                 GetComponent<Animator>().CrossFade("start", 0f);
    191.             }
    192.  
    193.         }
    194.  
    195.         //If the player has jumped but gone below the point in Y in which he should be dead
    196.         if (jumping == true && transform.position.y < gameOverY)
    197.         {
    198.             isDead = true;
    199.             GetComponent<Rigidbody>().velocity = Vector3.zero;
    200.             dir = Vector3.zero;
    201.  
    202.             deadFalling = true;
    203.         }
    204.  
    205.         //Check if dead and falling
    206.         if (deadFalling)
    207.         {
    208.             transform.GetChild(1).transform.parent = null;
    209.             dir = Vector3.zero;
    210.             //Stop Animation
    211.             GetComponent<Animator>().CrossFade("start", 0f);
    212.             //Increase falling speed
    213.             GetComponent<Rigidbody>().velocity = Vector3.down * 7;
    214.  
    215.             //Reload
    216.             StartCoroutine(gameover());
    217.             //startManager.GameOver(gameOverButtons, gameOverText, mainCam);
    218.  
    219.             if (score > PlayerPrefs.GetInt(highScoreKey))
    220.             {
    221.                 PlayerPrefs.SetInt(highScoreKey, score);
    222.                 PlayerPrefs.Save();
    223.             }
    224.         }
    225.  
    226.         //Check if dead
    227.         if (!Physics.Raycast(downRay, out hit) && jumping == false)
    228.         {
    229.             StartCoroutine(deadFall());
    230.         }
    231.  
    232.         //Roatation Animations
    233.         if (rotateLeft == true)
    234.         {
    235.             Quaternion newDir = Quaternion.AngleAxis(rotateA, Vector3.up);
    236.             child.localRotation = Quaternion.Lerp(child.rotation, newDir, rotationSpeed * Time.deltaTime);
    237.         }
    238.  
    239.         if (Mathf.Abs(rotateA - child.transform.localEulerAngles.y) < 2f)
    240.         {
    241.             rotateLeft = false;
    242.         }
    243.  
    244.         if (rotateRight == true)
    245.         {
    246.             Quaternion newDir2 = Quaternion.AngleAxis(rotateB, Vector3.up);
    247.             child.localRotation = Quaternion.Lerp(child.rotation, newDir2, rotationSpeed * Time.deltaTime);
    248.         }
    249.  
    250.         if (Mathf.Abs(rotateB-child.transform.localEulerAngles.y) < 2f)
    251.         {
    252.             rotateRight = false;
    253.         }
    254.  
    255.         float amountToMove = speed * Time.deltaTime;
    256.  
    257.         transform.Translate(dir * amountToMove);
    258.     }
    259.  
    260.     public void Jump()
    261.     {
    262.         RaycastHit hit;
    263.         Ray downRay = new Ray(new Vector3(transform.position.x - 0.485f, transform.position.y, transform.position.z + 0.485f), -transform.up);
    264.  
    265.         //Check if grounded
    266.         if (Physics.Raycast(downRay, out hit, rayHeight) && canJump)
    267.         {
    268.             goJump();
    269.         }
    270.  
    271.         //Can jump and change direction at the same time again
    272.         jump = false;
    273.     }
    274.  
    275.     //Go left
    276.     public void MoveLeft()
    277.     {
    278.             score++;
    279.             scoreText.text = score.ToString();
    280.             dir = Vector3.left;
    281.             tempDir = Vector3.left;
    282.  
    283.             rotateLeft = true;
    284.  
    285.         //(For mobile game) if the player has swiped left then jump as well
    286.         if (jump == true)
    287.         {
    288.             Jump();
    289.         }
    290.     }
    291.  
    292.     //Go Right
    293.     public void MoveRight()
    294.     {
    295.             score++;
    296.             scoreText.text = score.ToString();
    297.             dir = Vector3.forward;
    298.             tempDir = Vector3.forward;
    299.  
    300.             rotateRight = true;
    301.  
    302.         //(For mobile game) if the player has swiped right then jump as well
    303.         if (jump == true)
    304.         {
    305.             Jump();
    306.         }
    307.     }
    308.  
    309.     //Jump mechanics
    310.     public void goJump()
    311.     {
    312.             score++;
    313.             scoreText.text = score.ToString();
    314.  
    315.             if (dir == Vector3.forward)
    316.             {
    317.                 jumpPower = new Vector3(0, verticalJump * 2, 7);
    318.             }
    319.             else if (dir == Vector3.left)
    320.             {
    321.                 jumpPower = new Vector3(-7, verticalJump * 2, 0);
    322.             }
    323.  
    324.             dir = Vector3.zero;
    325.             rb.AddForce(jumpPower, ForceMode.VelocityChange);
    326.             jumping = true;
    327.     }
    328.  
    329.     //Start moving
    330.     public void StartMoving()
    331.     {
    332.         score++;
    333.         scoreText.text = score.ToString();
    334.  
    335.         dir = Vector3.forward;
    336.         tempDir = Vector3.forward;
    337.  
    338.         //Player anim
    339.         GetComponent<Animator>().CrossFade("RunRun", 0f);
    340.  
    341.         //The player can now jump, because the game has started
    342.         canJump = true;
    343.  
    344.         //(For mobile game) if the player has swiped left or right then jump as well
    345.         if (jump == true)
    346.         {
    347.             Jump();
    348.         }
    349.     }
    350.  
    351.     //Start falling after 0.1 seconds of being dead
    352.     IEnumerator deadFall()
    353.     {
    354.         yield return new WaitForSeconds(0.1f);
    355.         //The player is now dead
    356.         isDead = true;
    357.         //Make player fall faster
    358.         GetComponent<Rigidbody>().velocity = Vector3.zero;
    359.         //Player is not moving any more
    360.         dir = Vector3.zero;
    361.  
    362.         deadFalling = true;
    363.     }
    364.  
    365.     //Submit the score
    366.     public void SubmitScore()
    367.     {
    368.         //If a gameObject is found with the same name as the input fields text, then try again
    369.         GameObject nameTaken = GameObject.Find(name.text.ToString());
    370.  
    371.         if (nameTaken == null)
    372.         {
    373.             if (PlayerPrefs.GetInt("hasMadeName") == 0)
    374.             {
    375.                 //If the player has left the name input field blank
    376.                 if (name.text.ToString() == "")
    377.                 {
    378.                     //Change placeholder text
    379.                     inputPlaceHolder.text = "Enter a name!";
    380.                 }
    381.                 else
    382.                 {
    383.                     //Add high score if name isnt taken
    384.                     LeaderBoard.Instance.AddNewHighscore(name.text.ToString(), PlayerPrefs.GetInt(highScoreKey, 0));
    385.                     //Remember player name
    386.                     PlayerPrefs.SetString("PlayerName", name.text.ToString());
    387.                 }
    388.             }
    389.             //If player has already submitted thier score, and want to update it
    390.             else if (PlayerPrefs.GetInt("hasMadeName") == 1)
    391.             {
    392.                 //Add high score with remembered player name
    393.                 LeaderBoard.Instance.AddNewHighscore(PlayerPrefs.GetString("PlayerName"), PlayerPrefs.GetInt(highScoreKey, 0));
    394.             }
    395.  
    396.             //The player has made a name
    397.             PlayerPrefs.SetInt("hasMadeName", 1);
    398.         }
    399.         else
    400.         {
    401.             //If a name has been found, set the input field text to nothing and set the place holder
    402.             name.text = "";
    403.             inputPlaceHolder.text = "Name taken!";
    404.         }
    405.     }
    406.  
    407.     //Reload the game
    408.     IEnumerator gameover()
    409.     {
    410.         yield return new WaitForSeconds(1);
    411.         //Set score of gameOver UI
    412.         scoreBoardUI.transform.GetChild(1).GetComponent<Text>().text = score.ToString();
    413.         //Set highscore on GameOver UI
    414.         scoreBoardUI.transform.GetChild(2).GetComponent<Text>().text = PlayerPrefs.GetInt(highScoreKey, 0).ToString();
    415.         //Start GameOver GUI anims (see StartManager.cs)
    416.         startManager.GameOver(gameOverButtons, gameOverText, scoreBoardUI, scoreText, mainCam);
    417.  
    418.         Application.LoadLevel("scene1_Mobile");
    419.     }
    420.  
    421.     void OnTriggerEnter(Collider info)
    422.     {
    423.         //Collect coin
    424.         if(info.tag == "Coin")
    425.         {
    426.             info.gameObject.SetActive(false);
    427.             Instantiate(ps, transform.position, Quaternion.identity);
    428.  
    429.             score+= 3;
    430.             scoreText.text = score.ToString();
    431.  
    432.             //Make Combat Text
    433.             CombatTextManager.Instance.CreateText(transform.position, "+3", new Color32(255, 248, 0, 255), false);
    434.         }
    435.  
    436.         //Play the run animation and set jumping to false so that we know if the player falls off the Tile, he should be dead.
    437.         if (info.tag == "Tile")
    438.         {
    439.             dir = tempDir;
    440.             jumping = false;
    441.             GetComponent<Animator>().CrossFade("PlayerRun", 0f);
    442.         }
    443.     }
    444. }
    445.  
    StartManager.cs - Mainly handles GUI - Start menu and Gameover
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class StartManager : MonoBehaviour {
    6.  
    7.     //Public vars
    8.  
    9.     //Bools
    10.     public static bool startMoving;
    11.  
    12.     //Vector3s
    13.     public Vector3 dir;
    14.  
    15.     //Floats
    16.     public float speed;
    17.     public float positionEffectSpeed = 1.0f;
    18.     public float positionEffectDelay = 0f;
    19.     public float animTimeSpeed;
    20.  
    21.     //Gameobjects
    22.     public GameObject mainCam;
    23.     public GameObject scoreText;
    24.     public GameObject gameOverMenu;
    25.     public GameObject startMenu;
    26.     public GameObject leaderBoardMenu;
    27.  
    28.     //GUI
    29.  
    30.     //Texts
    31.     public Text startTxt;
    32.  
    33.     //RectTransforms
    34.     public RectTransform gameOverButtons;
    35.     public RectTransform gameOverText;
    36.     public RectTransform ryaoText;
    37.  
    38.     //Images
    39.     public Image backImage;
    40.  
    41.     //Private vars
    42.  
    43.     //Floats
    44.     private float amountToMove;
    45.  
    46.     //Bools
    47.     private static bool restartGame = false;
    48.     private bool loadLevel;
    49.  
    50.     //Other scripts can acces functions in this script
    51.     private static StartManager instance;
    52.  
    53.     public static StartManager Instance
    54.     {
    55.         get
    56.         {
    57.             if (instance == null)
    58.             {
    59.                 instance = GameObject.FindObjectOfType<StartManager>();
    60.             }
    61.             return StartManager.instance;
    62.         }
    63.     }
    64.  
    65.     // Use this for initialization
    66.     void Start () {
    67.         amountToMove = speed * Time.deltaTime;
    68.  
    69.         //If the game has not had the level reloaded
    70.         if (!loadLevel)
    71.         {
    72.             StartCoroutine(MoveUp());
    73.         }
    74.     }
    75.    
    76.     // Update is called once per frame
    77.     void Update () {
    78.  
    79.         //If the restart button has been hit
    80.         if (restartGame == true)
    81.         {
    82.             //Enable camera blur
    83.             Camera.main.gameObject.GetComponent<UnityStandardAssets.ImageEffects.BlurOptimized>().enabled = false;
    84.  
    85.             //Set start menu GUI to inactive
    86.             startMenu.SetActive(false);
    87.         }
    88.     }
    89.  
    90.     public void OnBackButtonClick()
    91.     {
    92.         //On leaderboard back button click
    93.         leaderBoardMenu.SetActive(false);
    94.         //Reload game (temporary)
    95.         reloadGame();
    96.     }
    97.  
    98.     //When the game starts, there is a small animation to move text from the center of the screen to the top, and to fade a white background into the start menu
    99.     IEnumerator MoveUp()
    100.     {
    101.         //Start MoveUp anim
    102.         yield return new WaitForSeconds(0.8f);
    103.         //LeenTween Anim
    104.         LeanTween.moveY(ryaoText.GetComponent<RectTransform>(), 310f, 0.8f);
    105.  
    106.         //Fade Background
    107.         backImage.CrossFadeAlpha(0, 0.5f, false);
    108.  
    109.         //Wait, then set backImage(Fading background upon start) to inactive
    110.         yield return new WaitForSeconds(1);
    111.         backImage.gameObject.SetActive(false);
    112.     }
    113.  
    114.     //On play button clicked
    115.     public void StartGame(Text scoreTextObject)
    116.     {
    117.         //Set start menu GUI to inactive
    118.         gameObject.transform.parent.transform.GetChild(3).transform.gameObject.SetActive(false);
    119.         //Set GUI score text to inactive
    120.         scoreText.gameObject.SetActive(true);
    121.  
    122.         //Get rid of cam blur
    123.         Camera.main.gameObject.GetComponent<UnityStandardAssets.ImageEffects.BlurOptimized>().enabled = false;
    124.         //Set score text to active
    125.         scoreTextObject.gameObject.SetActive(true);
    126.     }
    127.  
    128.     public void GameOver(RectTransform goButtons, RectTransform goText, RectTransform scoreBoard, Text scoreTextObject,  GameObject cameraMain)
    129.     {
    130.         restartGame = false;
    131.  
    132.         //Move Game Over Text (text that says "GameOver")
    133.         LeanTween.moveY(goText.gameObject.GetComponent<RectTransform>(), 546f, 0.3f);
    134.         //Move Game Over Buttons (Share, Restart, Leaderboard)
    135.         LeanTween.moveY(goButtons.GetComponent<RectTransform>(), -460f, 0.6f).setDelay(0.2f);
    136.         //Move scoreboard
    137.         LeanTween.moveY(scoreBoard.GetComponent<RectTransform>(), 87f, 0.6f).setDelay(0.1f);
    138.  
    139.         //scoreBoard.gameObject.transform.GetChild(2).
    140.  
    141.         Camera.main.gameObject.GetComponent<UnityStandardAssets.ImageEffects.BlurOptimized>().enabled = true;
    142.  
    143.         scoreTextObject.gameObject.SetActive(false);
    144.     }
    145.  
    146.     //When the level restarts
    147.     void OnLevelWasLoaded()
    148.     {
    149.         startMoving = true;
    150.  
    151.         //Set score text to active
    152.         scoreText.SetActive(true);
    153.     }
    154.  
    155.     //Reload the game on restart button click
    156.     public void reloadGame()
    157.     {
    158.         //Load scene again
    159.         Application.LoadLevel("scene1_Mobile");
    160.  
    161.         loadLevel = true;
    162.         restartGame = true;
    163.     }
    164. }
    165.  
    DisplayHighscores.cs - Handles Leaderboard GUI
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class DisplayHighscores : MonoBehaviour {
    6.  
    7.     //Public vars
    8.  
    9.     //Strings
    10.     string hasMadeName = "hasMadeName";
    11.  
    12.     //Scripts
    13.     LeaderBoard highscoreManager;
    14.  
    15.     //GameObjects
    16.     public GameObject[] highscores;
    17.     public GameObject fetchText;
    18.     public GameObject submitScore;
    19.     public GameObject submitScoreButton;
    20.     public GameObject startMenu, pauseMenu, leaderBoardMenu;
    21.  
    22.     //GUI
    23.  
    24.     //Texts
    25.     public Text highscoreText;
    26.     public Text[] highscoreText2;
    27.  
    28.     //RectTransforms
    29.     public RectTransform scoreView;
    30.  
    31.     //Private vars
    32.  
    33.     //GUI
    34.  
    35.     //Texts
    36.     private Text currentText;
    37.  
    38.     // Use this for initialization
    39.     void Start () {
    40.         //Initialize Array of Highscore texts
    41.         highscoreText2 = new Text[100];
    42.  
    43.         highscoreManager = GetComponent<LeaderBoard>();
    44.  
    45.         //StartCoroutine(RefreshHighscores());
    46.     }
    47.  
    48.     //When the highscores have been downloaded
    49.     public void OnHighscoresDownloaded(LeaderBoard.Highscore[] highscoreList)
    50.     {
    51.         int numOfScores = highscoreList.Length;
    52.  
    53.         if (PlayerPrefs.GetInt(hasMadeName) == 1)
    54.         {
    55.             submitScoreButton.SetActive(true);
    56.         }
    57.         else
    58.         {
    59.             submitScore.SetActive(true);
    60.         }
    61.  
    62.         fetchText.SetActive(false);
    63.  
    64.         for (int i = 0; i < numOfScores; i++)
    65.         {
    66.             Vector3 nextPosInit = new Vector3(-165, highscoreText.rectTransform.localPosition.y, 0);
    67.  
    68.             if (i == 0)
    69.             {
    70.                 highscoreText2[i] = Instantiate(highscoreText, nextPosInit, Quaternion.identity) as Text;
    71.             }
    72.             else if (i > 0)
    73.             {
    74.                 Vector3 nextPos = new Vector3(-165, highscoreText2[i - 1].rectTransform.localPosition.y - 50, 0);
    75.  
    76.                 highscoreText2[i] = Instantiate(highscoreText, nextPos, Quaternion.identity) as Text;
    77.             }
    78.  
    79.             currentText = highscoreText2[i];
    80.  
    81.             currentText.text = i + 1 + ". " + highscoreList[i].username;
    82.             currentText.transform.GetChild(0).GetComponent<Text>().text = highscoreList[i].score.ToString();
    83.             currentText.name = highscoreList[i].username;
    84.  
    85.             currentText.transform.SetParent(scoreView, false);
    86.         }
    87.     }
    88.  
    89.     //Note: Fix double instantiation of scores after 30 seconds
    90.     IEnumerator RefreshHighscores()
    91.     {
    92.         while (true)
    93.         {
    94.             highscoreManager.DownloadHighscores();
    95.             yield return new WaitForSeconds(30);
    96.         }
    97.     }
    98.  
    99.     public void StartGui()
    100.     {
    101.         highscoreManager.DownloadHighscores();
    102.  
    103.         //GUI to submit highscore to leaderboard
    104.         submitScore.SetActive(false);
    105.         //Submit score button
    106.         submitScoreButton.SetActive(false);
    107.         //Start Menu Gui
    108.         startMenu.SetActive(false);
    109.         //GameOver GUI - Accidentally named it pause menu
    110.         pauseMenu.SetActive(false);
    111.         //Leaderboard GUI
    112.         leaderBoardMenu.SetActive(true);
    113.         //Text to say "Fetching..."
    114.         fetchText.SetActive(true);
    115.     }
    116.  
    117.     // Update is called once per frame
    118.     void Update () {
    119.    
    120.     }
    121. }
    122.  
    Then again, all of this might be irrelevant and this crashing is a bug in Unity 5's (5.2.2f1 personal) GUI system.

    Thanks to anyone who takes the time to read this and respond.
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Did you take a look at your log? It FULL of Null ref exceptions and Transform child out of bounds exceptions. I can't be certain but i see nothing UI related that would cause the crashes. If i were you I'd take a look at cleaning up your exceptions.
     
  3. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    I really don't know how.. I mean I've cleaned up a few of them but since the code is mostly in update there are null ref exceptions and child out of bounds errors everywhere.

    And I'm pretty sure it is to do with the UI, because if I remove the UI, I get the same errors but it doesn't crash.

    I'll take a look at the code, but thank you anyway.
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Well its hard to say if its due to the errors with all those null refs as it might be hiding something. So its where i'd start.