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

List In Inspector is Null on iOS Device

Discussion in 'iOS and tvOS' started by Ben-BearFish, Jun 11, 2014.

  1. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    In the editor we add gameobjects to a C# list on a component script. Then save the scene. When we run the scene in the editor we can access the gamobjects from the C# list, but when we run the scene on the iPhone the list is null on Awake(). Has anyone else had this problem and is have you found a solution? It's weird that the list is null on iOS but not the editor.
     
  2. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    We're using Unity 4.5, and xCode 5.1.1.
     
  3. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Could you please post code sample how exactly you have implemented it?
     
  4. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
    If you have different public variables in Platform Defines, you have to bind them again when you change Unity build platform, e.g.
    Code (csharp):
    1.  
    2. #if UNITY_EDITOR
    3.      public GameObject editorGo;
    4.     #endif
    5.    
    6.     #if UNITY_IPHONE
    7.     public GameObject iosGo;
    8.     #endif
    9.  
    10.     #if UNITY_ANDROID
    11.     public GameObject androidGo;
    12.     #endif
    13.  
    you have that code in project saved as Android project, and you change build platform to iPhone, then iosGo will be null.
     
  5. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Sure thing. It's a script we use to setup the scene. In the Editor we have it ExecuteInEditMode, which then looks for gameobjects in the scene in EditorMode and adds them to its list. When we save the scene, then play the scene in the editor it works, but in iOS the list seems to be null.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Collections.Generic;
    5.  
    6. #if UNITY_EDITOR
    7. using UnityEditor;
    8. #endif
    9.  
    10. #if UNITY_EDITOR
    11. [ExecuteInEditMode]
    12. #endif
    13. public class MissionSetup : MonoBehaviour
    14. {
    15.     protected static MissionSetup instance = null;
    16.     public static MissionSetup Instance
    17.     {
    18.         get
    19.         {
    20.             if ( instance == null )
    21.             {
    22.                 instance = FindObjectOfType ( typeof ( MissionSetup ) ) as MissionSetup;
    23.             }
    24.             return instance;
    25.         }
    26.     }
    27.  
    28.     [SerializeField]
    29.     protected List<MissionObjective> objectivesList;
    30.    
    31.     [SerializeField]
    32.     protected List<RoomObject> roomObjectsList;
    33.     public RoomObject[] RoomObjectsForMission
    34.     {
    35.         get
    36.         {
    37.             if(roomObjectsList != null)
    38.                 return roomObjectsList.ToArray();
    39.             return null;
    40.         }
    41.     }
    42.  
    43.     protected void Awake()
    44.     {
    45.         #if UNITY_EDITOR
    46.         if(!Application.isPlaying && Application.isEditor)
    47.         {
    48.             return;
    49.         }
    50.         #endif
    51.        
    52.         Managers.SetMissionSetup(this);
    53.     }
    54.  
    55.     protected void OnEnable ()
    56.     {
    57.         #if UNITY_EDITOR
    58.         if(!Application.isPlaying && Application.isEditor)
    59.         {
    60.             //ADD THE ROOM OBJECTS BEING ADDED IN THE EDITOR HERE
    61.             if(roomObjectsList != null)
    62.                 roomObjectsList.Clear();
    63.             if(objectivesList != null)
    64.                 objectivesList.Clear();
    65.         }
    66.         #endif
    67.     }
    68.    
    69.     protected void GetAllRoomObjects()
    70.     {
    71.         RoomObject[] roomObjectsInLevel = FindObjectsOfType ( typeof ( RoomObject ) ) as RoomObject[];
    72.         if(roomObjectsInLevel != null && roomObjectsInLevel.Length > 0)
    73.         {
    74.             int dialogueObjectsIndex = -1;
    75.             int roomObjectsIndex = -1;
    76.             int roomUnlockIndex = -1;
    77.             if(roomsToUnlockList != null)
    78.                 roomsToUnlockList.Clear();
    79.             int length = roomObjectsInLevel.Length;
    80.             for(int i = 0; i < length; ++i)
    81.             {
    82.                 if(roomObjectsInLevel[i] != null && roomObjectsInLevel[i].gameObject.transform.root.GetComponent<AbsurdDestroyOnAwake>() == null)
    83.                 {
    84.                     //Adds the room objects in level
    85.                     roomObjectsIndex = roomObjectsList.IndexOf(roomObjectsInLevel[i]);
    86.                     if(roomObjectsIndex == -1)
    87.                     {
    88.                         roomObjectsList.Add(roomObjectsInLevel[i]);
    89.                     }
    90.                 }
    91.             }
    92.         }
    93.        
    94.         if(roomObjectsList != null && roomObjectsList.Count > 0)
    95.         {
    96.             roomObjectsList.RemoveAll(delegate (RoomObject ro) { return ro == null; });
    97.             roomObjectsList.RemoveAll(delegate (RoomObject ro) { return ro.gameObject.activeInHierarchy == false; });
    98.             roomObjectsList.RemoveAll(delegate (RoomObject ro) { return ro.gameObject.activeSelf == false; });
    99.         }
    100.        
    101.        
    102.         EditorUtility.SetDirty(this);
    103.     }
    104.    
    105.     protected void GetAllMissionObjectives()
    106.     {
    107.         MissionObjective[] missionObjectivesInLevel = FindObjectsOfType ( typeof ( MissionObjective ) ) as MissionObjective[];
    108.         if(missionObjectivesInLevel != null && missionObjectivesInLevel.Length > 0)
    109.         {
    110.             int missionObjectiveIndex = -1;
    111.             int length = missionObjectivesInLevel.Length;
    112.             for(int i = 0; i < length; ++i)
    113.             {
    114.                 if(missionObjectivesInLevel[i] != null)
    115.                 {
    116.                     missionObjectiveIndex = objectivesList.IndexOf(missionObjectivesInLevel[i]);
    117.                     if(missionObjectiveIndex == -1)
    118.                     {
    119.                         objectivesList.Add(missionObjectivesInLevel[i]);
    120.                     }
    121.                 }
    122.             }
    123.         }
    124.        
    125.         if(objectivesList != null && objectivesList.Count > 0)
    126.         {
    127.             objectivesList.RemoveAll(delegate (MissionObjective mo) { return mo == null; });
    128.             objectivesList.RemoveAll(delegate (MissionObjective mo) { return mo.gameObject.activeInHierarchy == false; });
    129.             objectivesList.RemoveAll(delegate (MissionObjective mo) { return mo.gameObject.activeSelf == false; });
    130.         }
    131.        
    132.         EditorUtility.SetDirty(this);
    133.     }
    134.  
    135.     protected void OnDrawGizmos()
    136.     {
    137.         #if UNITY_EDITOR
    138.         if(Application.isEditor && Application.isPlaying)
    139.             return;
    140.        
    141.         GetAllMissionObjectives();
    142.         GetAllRoomObjects();
    143.        
    144.         #endif
    145.     }
    146.  
    147. }
    148.  
     
  6. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Could you also add MissionObjective and RoomObject declarations?
     
  7. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Here's the MissionObjective.cs code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public enum MissionObjectiveState
    6. {
    7.     Incomplete = 0, Failed = 1, Complete = 2
    8. }
    9.  
    10. public enum MissionObjectiveType
    11. {
    12.     Primary = 0, Secondary = 1, Bonus = 2
    13. }
    14.  
    15. public class MissionObjective : MonoBehaviour, IComparable<MissionObjective>
    16. {
    17.     protected MissionObjectiveState currentState = MissionObjectiveState.Incomplete;
    18.  
    19.     public MissionObjectiveState ObjectiveStatus
    20.     {
    21.         get
    22.         {
    23.             return currentState;
    24.         }
    25.     }
    26.  
    27.     public RoomTypes roomThatContainsObjective = RoomTypes.None;
    28.     public MissionObjectiveType Type = MissionObjectiveType.Primary;
    29.  
    30.     public int MissionObjectiveSortOrder = 0;
    31.  
    32.     protected int numberOfThisObjectiveInMission = 1;
    33.     public int NumberOfThisObjectiveInMission
    34.     {
    35.         get
    36.         {
    37.             return numberOfThisObjectiveInMission;
    38.         }
    39.         set
    40.         {
    41.             numberOfThisObjectiveInMission = value;
    42.         }
    43.     }
    44.  
    45.     public int CompareTo(MissionObjective other)
    46.     {
    47.  
    48.         return this.MissionObjectiveSortOrder.CompareTo(other.MissionObjectiveSortOrder);
    49.     }
    50.  
    51.     protected void OnDestroy()
    52.     {
    53.         Managers.MISSION.RemoveObjectiveToMission(this);
    54.     }
    55.  
    56.     public void SetMissionObjectiveStatus(MissionObjectiveState isComplete)
    57.     {
    58.         currentState = isComplete;
    59.         switch(isComplete)
    60.         {
    61.             case MissionObjectiveState.Complete:
    62.             {
    63.                 if(Managers.MISSION.CheckAreAllObjectivesComplete())
    64.                 {
    65.                     Managers.MISSION.TriggerMissionComplete();
    66.                 }
    67.                 break;
    68.             }
    69.             case MissionObjectiveState.Failed:
    70.             {
    71.                 Managers.MISSION.TriggerMissionFailed();
    72.                 break;
    73.             }
    74.         }
    75.     }
    76. }
    Here's the RoomObject.cs code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. #if UNITY_EDITOR
    5. using UnityEditor;
    6. #endif
    7.  
    8. public class RoomObject : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     protected RoomTypes roomThatContainsObject;
    12.     public RoomTypes RoomThatContainsObject
    13.     {
    14.         get
    15.         {
    16.             return roomThatContainsObject;
    17.         }
    18.     }
    19.  
    20.     protected Vector2 startPosition = Vector2.zero;
    21.  
    22.     protected virtual void ResetPosition()
    23.     {
    24.         if(startPosition != Vector2.zero)
    25.         {
    26.             transform.position = startPosition;
    27.         }
    28.     }
    29.  
    30.     public virtual void ActivateRoomObject(bool activate)
    31.     {
    32.         if(transform.root != Managers.PERSISTENT.transform)
    33.             transform.root.gameObject.SetActive(activate);
    34.     }
    35.  
    36.     public void SetRoom()
    37.     {
    38.         Vector2 pivotPoint = transform.position;
    39.    
    40.         LayerMask raycastMask = LayersExtension.NamesToMask("Spaceship Environment", "Room Exits");
    41.         RaycastHit2D hitUp = Physics2D.Raycast(pivotPoint, Vector2.up, 1000.0f, raycastMask);
    42.         RaycastHit2D hitRight = Physics2D.Raycast(pivotPoint, Vector2.right, 1000.0f, raycastMask);
    43.    
    44.         if(hitUp.collider != null && hitUp.collider.transform.parent != null)
    45.         {
    46.             RoomLayer layer = hitUp.collider.transform.parent.GetComponent<RoomLayer>();
    47.             if(layer != null)
    48.             {
    49.                 if(roomThatContainsObject != layer.RoomType)
    50.                 {
    51.                     roomThatContainsObject = layer.RoomType;
    52.                     #if UNITY_EDITOR
    53.                     EditorUtility.SetDirty(this);
    54.                     #endif
    55.                 }
    56.             }
    57.         }
    58.    
    59.         if(hitRight.collider != null && hitRight.collider.transform.parent != null)
    60.         {
    61.             RoomLayer layer = hitRight.collider.transform.parent.GetComponent<RoomLayer>();
    62.             if(layer != null)
    63.             {
    64.                 if(roomThatContainsObject != layer.RoomType)
    65.                 {
    66.                     roomThatContainsObject = layer.RoomType;
    67.                     #if UNITY_EDITOR
    68.                     EditorUtility.SetDirty(this);
    69.                     #endif
    70.                 }
    71.             }
    72.         }
    73.     }
    74.  
    75.     protected virtual void OnDrawGizmos()
    76.     {
    77.         if(Application.isEditor && Application.isPlaying)
    78.             return;
    79.    
    80.         SetRoom();
    81.     }
    82.  
    83.     protected virtual void OnEnable()
    84.     {
    85.    
    86.     }
    87.  
    88.     protected virtual void OnDisable()
    89.     {
    90.    
    91.     }
    92. }
     
  8. Smilediver

    Smilediver

    Joined:
    May 5, 2011
    Posts:
    72
    You probably need to mark your classes with [Serializable] attribute.
     
  9. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    I set all my classes to Serializeable, but it seems the lists are still null on Start.