Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Crash when Instantiating GameObjects, only on iOS

Discussion in '2023.2 Beta' started by harry_unity450, Oct 9, 2023.

  1. harry_unity450

    harry_unity450

    Joined:
    May 25, 2023
    Posts:
    3
    Hi, I'm currently using 2023.2.0b11 and running into an issue with all of the latest betas (and the latest alpha) during runtime only on iOS builds, where-in the game just hangs on this line:

    Code (CSharp):
    1. ball = Instantiate<BallView>(_ballPrefab, parentContainer);
    In this code:

    Code (CSharp):
    1.  
    2. using DG.Tweening;
    3. using EyegazeCore;
    4. using System;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7. using System.Linq;
    8. using UnityEngine;
    9. using UnityEngine.Events;
    10.  
    11. public class BallReleaseController : MonoBehaviour
    12. {
    13.     private List<BallReleaser> _releasers = new List<BallReleaser>();
    14.  
    15.     public IntEvent ON_BALL_RELEASE = new IntEvent();
    16.  
    17.     [SerializeField]
    18.     private BallView _ballPrefab;
    19.  
    20.     [SerializeField]
    21.     private Transform _ballContainer;
    22.  
    23.     private List<BallView> _activeBalls = new List<BallView>();
    24.  
    25.     private Dictionary<Vector2Int, BallView> _released = new Dictionary<Vector2Int, BallView>();
    26.  
    27.     private Sequence _deal, _reject;
    28.     private BoardNodes _nodes;
    29.  
    30.     internal void Init(BoardNodes nodes)
    31.     {
    32.         _nodes = nodes;
    33.     }
    34.  
    35.     private void Start()
    36.     {
    37.         float yAnchor = 0;
    38.         _releasers = GetComponentsInChildren<BallReleaser>().ToList();
    39.         for (int i = 0; i < _releasers.Count; i++){
    40.             BallReleaser r = _releasers[i];
    41.             yAnchor = r.transform.localPosition.y;
    42.             r.Init(i).ON_RELEASE.AddListener(OnRelease);
    43.             r.transform.localPosition = new Vector3(r.transform.localPosition.x, r.transform.localPosition.y * -1);
    44.  
    45.             r.transform.DOLocalMoveY(yAnchor, 1f).SetEase(Ease.OutCubic).SetDelay(i * .18f).Play();
    46.         }
    47.     }
    48.  
    49.     private void OnRelease(int i)
    50.     {
    51.         //animation
    52.         _releasers[i].OnRelease();
    53.  
    54.         ON_BALL_RELEASE.Invoke(i);
    55.     }
    56.  
    57.     public void Init(Board board, FourInARowSession session, BallColor player1Color, BallColor player2Color)
    58.     {
    59.         for (int x = 0; x < board.xBounds; x++){
    60.             for (int y = 0; y < board.yBounds; y++){
    61.                 int move = board.Matrix[x][y];
    62.                 if(move != 0){
    63.                     var ball = Instantiate<BallView>(_ballPrefab, _nodes.GetContainer(x)).Init(move == 1 ? player1Color : player2Color);
    64.                     ball.transform.localPosition = _nodes.GetLocalWorldPosition(x,y);
    65.                 }
    66.             }
    67.         }
    68.     }
    69.  
    70.     //Static
    71.     public void Release(Vector2Int pos, BallColor color)
    72.     {
    73.         // Get the parent container for the new BallView object
    74.         Transform parentContainer = _nodes.GetContainer(pos.x);
    75.  
    76.         // Instantiate a new BallView object as a child of the obtained container
    77.         BallView ball = Instantiate<BallView>(_ballPrefab, parentContainer);
    78.  
    79.         // Initialize the color of the new BallView object
    80.         ball.Init(color);
    81.  
    82.         ball.transform.position = _nodes.GetLocalWorldPosition(pos.x, pos.y);
    83.  
    84.         _released.Add(pos, ball);
    85.  
    86.         //Clear unpicked
    87.         for (var i = 0; i < _activeBalls.Count; i++){
    88.             BallView t = _activeBalls[i];
    89.             if (t != null){
    90.                 Destroy(t.gameObject);
    91.                 t = null;
    92.             }
    93.         }
    94.  
    95.         _activeBalls.Clear();
    96.     }
    97.  
    98.     //Animated
    99.     public void Release(Vector2Int pos, TweenCallback OnCompleteCallback)
    100.     {
    101.    
    102.         if (_activeBalls[pos.x] != null){
    103.             BallView ball = _activeBalls[pos.x];
    104.             DOTween.Kill(ball);
    105.  
    106.             ball.transform.DOLocalMove(_nodes.GetLocalWorldPosition(pos.x, pos.y), Vector3.Distance(ball.transform.localPosition, _nodes.GetLocalWorldPosition(pos.x, pos.y)) * 0.0009f).SetEase(Ease.OutBounce).Play().OnComplete(OnCompleteCallback);
    107.  
    108.             _released.Add(pos, ball);
    109.             _activeBalls[pos.x] = null;
    110.         }
    111.  
    112.         Clear();
    113.     }
    114.  
    115.     private void Clear()
    116.     {
    117.         Debug.Log("3 Clearing Deal");
    118.         for (var i = 0; i < _activeBalls.Count; i++){
    119.             BallView t = _activeBalls[i];
    120.             if (t != null){
    121.                 if (t != null){
    122.                     Destroy(t.gameObject);
    123.                     t = null;
    124.                 }
    125.             }
    126.         }
    127.  
    128.         _activeBalls.Clear();
    129.     }
    130.  
    131.     internal Sequence Deal(List<int> availablePlays, BallColor color)
    132.     {
    133.         Debug.Log("Dealing");
    134.         Clear();
    135.  
    136.         if (_deal != null){
    137.             _deal.Kill();
    138.             _deal = null;
    139.         }
    140.  
    141.         _deal = DOTween.Sequence();
    142.  
    143.         for (int i = 0; i < _releasers.Count; i++){
    144.             Vector3 dest = _nodes.GetDropPosition(i);
    145.             Vector3 from = dest + Vector3.up * 250;
    146.            
    147.             // Step 1: Conditional Check
    148.             Debug.Log("1 Checking if should instantiate ball");
    149.             bool shouldInstantiateBall = availablePlays[i] != -1;
    150.             Debug.Log("1.1 should instantiate ball is " + shouldInstantiateBall);
    151.             // Step 2: Get Container
    152.             Debug.Log("2 Getting Container");
    153.             Transform parentContainer = null;
    154.             if (shouldInstantiateBall) {
    155.                 Debug.Log("2.1 getting container");
    156.                 parentContainer = _nodes.GetContainer(i);
    157.                 Debug.Log("2.2 got container");
    158.             }
    159.  
    160.             // Step 3: Instantiate and Initialize BallView
    161.             BallView ball = null;
    162.  
    163.             if (shouldInstantiateBall){
    164.                 Debug.Log("3.1 instantiating ball");
    165.                 ball = Instantiate<BallView>(_ballPrefab, parentContainer);
    166.                 Debug.Log("3.2 initializing ball");
    167.                 ball.Init(color);
    168.                 Debug.Log("3.3 initilized ball");
    169.             }
    170.  
    171.             _activeBalls.Add(ball);
    172.  
    173.             if (ball != null){
    174.                 ball.transform.localPosition = from;
    175.                 _deal.Insert(0, ball.transform.DOLocalMove(dest, 1f).SetEase(Ease.OutBounce));//.SetDelay(UnityEngine.Random.Range(.5f, .8f)));
    176.             }
    177.         }
    178.  
    179.         return _deal.Play();
    180.     }
    181.  
    182.     public void OnGameWon(List<Vector2Int> winners)
    183.     {
    184.         // var s = DOTween.Sequence();
    185.         foreach (Vector2Int ball in _released.Keys){
    186.             if (winners.Contains(ball)) _released[ball].state = BallState.Glow;
    187.             else _released[ball].state = BallState.Smash;
    188.         }
    189.     }
    190.  
    191.     public bool interactable
    192.     {
    193.         set {
    194.             foreach (BallReleaser r in _releasers) r.interactable = value;
    195.         }
    196.     }
    197. }
    198.  
    When built in development mode, the game freezes, but after 10 seconds or so continues. In release mode the game just freezes never to play again.

    This does not happen on WebGL or Android builds, or when played within the editor.

    If I downgrade to a 2022 build, the game also runs as expected. We are using the beta to access the new accessibility API features.

    Other than rewrite the game code to avoid the instantiation, which I'd rather not do, I'm at a bit of a loss. Any help would be much appreciated.

    Thanks
    Harry