Search Unity

Prefab instantiation not working on iOS (working fine in editor)

Discussion in 'Editor & General Support' started by Benfont, Dec 5, 2018.

  1. Benfont

    Benfont

    Joined:
    Nov 11, 2017
    Posts:
    21
    hey guys,

    I have been trying to solve this problem for some time now and I cannot find a way to fix it..

    I have a script that instantiates prefabs at a specific location in my game.
    The script works perfectly in the editor, but when I build the game for iOS there is no instantiation going on (I do not get the prefabs instantiated - no sprite rendered nor collision boxes).
    I checked if the problem were the assets, so I created a new scene and manually placed the assets there - the assets in those scenes are rendering just fine (all their collision boxes and physics are working fine too), my problem is to instantiate the prefabs at runtime on my built game.

    I am using Unity 2018.2.1 and Xcode Version 10.1 (10B61) and have tested the game in a few iOS devices, including iPads.

    Has anybody faced a similar issue before?

    Here is my code (thanks in advance for your help!):

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TileManager : MonoBehaviour
    7. {
    8.     public GameObject[] tiles;
    9.     public Transform playerTransform;
    10.     private List<GameObject> activeTiles;
    11.  
    12.     float spawnY = 8f;
    13.     float tileLenght = 6.4f;
    14.     int lastTileIndex;
    15.  
    16.     void Awake() { activeTiles = new List<GameObject>(); }
    17.     void Start() { SpawnTile(); }
    18.  
    19.     private void Update()
    20.     {
    21.         if (GameObject.FindGameObjectsWithTag("Tile").Length < 3)
    22.         { SpawnTile(); }
    23.     }
    24.  
    25.     private void SpawnTile()
    26.     {
    27.         GameObject go;
    28.         go = Instantiate(tiles[RandomTileIndex()] as GameObject);
    29.         go.transform.SetParent(transform);
    30.         go.transform.position = Vector3.down * spawnY;
    31.         spawnY += tileLenght;
    32.         activeTiles.Add(go);
    33.     }
    34.  
    35.     private int RandomTileIndex()
    36.     {
    37.         if (tiles.Length <= 1)
    38.             return 0;
    39.         int randomIndex = lastTileIndex;
    40.         while (randomIndex == lastTileIndex)
    41.         { randomIndex = Random.Range(1, tiles.Length); }
    42.         lastTileIndex = randomIndex;
    43.         return randomIndex;
    44.     }
    45. }
    46.  
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    When you build a test out for a device from Xcode it should auto connect to the device and capture logs from the game. Do you get any error messages in the logs?

    Also just to check - the proper scenes are added to the build? Sometimes I get problems when I update a scene and name it a new file, without adding it to the build settings window.
     
    Benfont likes this.
  3. Benfont

    Benfont

    Joined:
    Nov 11, 2017
    Posts:
    21
    Hello @MD_Reptile thanks for replying..
    yes I have added the scenes to the build and they have not been renamed since I created them.
    And no, no errors that I can tell so far in the xcode log,

    This is what I get:



    -> applicationDidFinishLaunching()

    2018-12-05 06:24:53.730291+0000 snow2[52733:4007020] Metal GPU Frame Capture Enabled

    2018-12-05 06:24:53.730690+0000 snow2[52733:4007020] Metal API Validation Disabled

    2018-12-05 06:24:53.954099+0000 snow2[52733:4007020] [Warning] Trying to set delaysTouchesBegan to NO on a system gate gesture recognizer - this is unsupported and will have undesired side effects

    -> applicationDidBecomeActive()

    GfxDevice: creating device client; threaded=1

    Initializing Metal device caps: Apple A11 GPU

    Initialize engine version: 2018.2.1f1 (1a9968d9f99c)

    UnloadTime: 0.444000 ms

    Setting up 1 worker threads for Enlighten.

    Thread -> id: 170977000 -> priority: 1

    Unloading 5 Unused Serialized files (Serialized files now loaded: 0)

    UnloadTime: 1.578541 ms



    Unloading 248 unused Assets to reduce memory usage. Loaded Objects now: 2775.

    Total: 6.079791 ms (FindLiveObjects: 1.015875 ms CreateObjectMapping: 0.040291 ms MarkObjects: 4.681666 ms DeleteObjects: 0.340666 ms)



    Unloading 2 Unused Serialized files (Serialized files now loaded: 0)

    UnloadTime: 11.209125 ms



    Unloading 283 unused Assets to reduce memory usage. Loaded Objects now: 1161.

    Total: 3.716458 ms (FindLiveObjects: 0.927500 ms CreateObjectMapping: 0.023250 ms MarkObjects: 1.722291 ms DeleteObjects: 1.042625 ms)
     
  4. Benfont

    Benfont

    Joined:
    Nov 11, 2017
    Posts:
    21
    OK I found what the issue was (thanks to your suggestion of checking the log in xcode @MD_Reptile )

    I googled what this line was:
    Unloading 248 unused Assets to reduce memory usage. Loaded Objects now: 2775.

    and found the following thread

    https://forum.unity.com/threads/prevent-xcode-from-removing-an-object-as-unused.482299/

    there one guy suggest removing all the gameobjects containing the code and adding them again.
    This fixed my issue.

    to be honest I still don't know what I did incorrectly, but deleting the code from the hierarchy and adding it again fixed the problem for me.
    I hope this helps someone else..

    thanks again md reptile!
     
    MD_Reptile likes this.
  5. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Ahh - thats tricky! Well good job sorting it out, and thanks for sharing what worked for you.

    Good luck!
     
    Benfont likes this.