Search Unity

Unity Build runs errors while the editor does not.

Discussion in 'Editor & General Support' started by Spares, Dec 14, 2020.

  1. Spares

    Spares

    Joined:
    Apr 19, 2019
    Posts:
    3
    I'm having some trouble with the build of my Unity project.

    The code that enables/disables the compartment gameobjects of my base work fine in the editor, however when running the build they sometimes enable child gameobjects from a spawn gameobject other than the one being hit by the raycast. When I save and change scenes, then load back into the base scene, the enabled gameobjects have been switched to other spawn gameobjects. Also when trying to click through the child gameobjects of the spawners, it will only cycle to the next 1. When I reload the scene, then I can cycle 1 more.

    This would be less frustrating if the same thing happened in the editor. At least then I could debug it.

    Any theories will be appreciated. I'm fairly new at this and my coding skills are pretty much trial and error at this point.

    Also, feel free to point out any coding mistakes I've made as long as it doesn't get too complicated.

    Thanks.


    Code (CSharp):
    1. public class BaseManager : MonoBehaviour
    2. {
    3.  
    4.     public GameManager gameM;
    5.     public Player player;
    6.  
    7.     public GameObject hitObject;
    8.     static public Transform modularCompartments;
    9.     string[] CompartmentType;
    10.  
    11.  
    12.     GameObject[] wreckage;
    13.     GameObject[] cleared;
    14.     GameObject[] barracks;
    15.     GameObject[] engineering;
    16.     GameObject[] laboratory;
    17.  
    18.     public string tagged;
    19.     GameObject[] spawners;
    20.  
    21.     void Start()
    22.     {
    23.         player = GameObject.Find("Player").GetComponent<Player>();
    24.         CompartmentType = new string[] { "SpawnComp", "Wreckage", "Cleared", "Barracks", "Engineering", "Laboratory" };
    25.         spawners = GameObject.FindGameObjectsWithTag("SpawnComp");
    26.      
    27.         StartCoroutine(ABC());
    28.     }
    29.  
    30.      IEnumerator ABC()
    31.      {
    32.          yield return null;
    33.  
    34.          PopulateCompartments();
    35.  
    36.      }
    37.  
    38.     void Update()
    39.     {ObjectClick(); }
    40.  
    41.     void ObjectClick()
    42.     {
    43.  
    44.         //On click grabs the spawn point gameobject and disables the current gameobject
    45.         //in it's child array and enables the next one.
    46.  
    47.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    48.  
    49.  
    50.         //Grabs the save game compartments
    51.         for (int i = 0; i < player.tags.Length; i++)
    52.         { spawners[i].GetComponent<Spawner>().tagg = player.tags[i]; }
    53.  
    54.  
    55.         if (Physics.Raycast(ray, out RaycastHit hitInfo))
    56.         {
    57.             hitObject = hitInfo.collider.transform.gameObject;
    58.  
    59.             if (Input.GetMouseButtonDown(0))
    60.  
    61.             {
    62.                // Debug.Log("RayCast = " + hitObject);
    63.                 for (int i = 0; i < player.tags.Length; i++)
    64.                 {
    65.                     tagged = player.tags[i];
    66.                     string sp = "SP" + (i + 1);
    67.  
    68.                     if (sp == hitObject.name)
    69.                     {
    70.                         if (hitObject.GetComponent<Spawner>().tagg == CompartmentType[5])
    71.                         {
    72.                             laboratory = hitObject.GetComponent<Spawner>().SP;
    73.  
    74.                             foreach (GameObject l in laboratory)
    75.                             {
    76.                                 l.SetActive(false);
    77.                                 if (l.name == CompartmentType[1])
    78.                                 {
    79.                                     l.SetActive(true);
    80.                                 }
    81.  
    82.                             }
    83.                             hitObject.GetComponent<Spawner>().tagg = CompartmentType[1];
    84.                             tagged = CompartmentType[1];
    85.                             player.tags[i] = tagged;
    86.                             player.SavePlayer();
    87.                             break;
    88.                         }
    89.  
    90.  
    91.                         else if (hitObject.GetComponent<Spawner>().tagg == CompartmentType[4])
    92.                         {
    93.                             engineering = hitObject.GetComponent<Spawner>().SP;
    94.  
    95.                             foreach (GameObject e in engineering)
    96.                             {
    97.                                 e.SetActive(false);
    98.                                 if (e.name == CompartmentType[5])
    99.                                 {
    100.                                     e.SetActive(true);
    101.                                 }
    102.  
    103.                             }
    104.                             hitObject.GetComponent<Spawner>().tagg = CompartmentType[5];
    105.                             tagged = CompartmentType[5];
    106.                             player.tags[i] = tagged;
    107.                             player.SavePlayer();
    108.                             break;
    109.                         }
    110.                         else if (hitObject.GetComponent<Spawner>().tagg == CompartmentType[3])
    111.                         {
    112.  
    113.                             barracks = hitObject.GetComponent<Spawner>().SP;
    114.  
    115.                             foreach (GameObject b in barracks)
    116.                             {
    117.                                 b.SetActive(false);
    118.                                 if (b.name == CompartmentType[4])
    119.                                 {
    120.                                     b.SetActive(true);
    121.                                 }
    122.  
    123.                             }
    124.                             hitObject.GetComponent<Spawner>().tagg = CompartmentType[4];
    125.                             tagged = CompartmentType[4];
    126.                             player.tags[i] = tagged;
    127.                             player.SavePlayer();
    128.                             break;
    129.                         }
    130.  
    131.                         else if (hitObject.GetComponent<Spawner>().tagg == CompartmentType[2])
    132.                         {
    133.                             //Debug.Log("Cleared");//     MouseOver_Unit(ourHitObject);
    134.  
    135.  
    136.                             cleared = hitObject.GetComponent<Spawner>().SP;
    137.                             //Debug.Log("Cleared Length = " + cleared.Length);
    138.  
    139.                             foreach (GameObject c in cleared)
    140.                             {c.SetActive(false);
    141.  
    142.                                 if (c.name == CompartmentType[3])
    143.                                 {c.SetActive(true);}
    144.  
    145.                             }
    146.                             hitObject.GetComponent<Spawner>().tagg = CompartmentType[3];
    147.                             tagged = CompartmentType[3];
    148.                             player.tags[i] = tagged;
    149.                             player.SavePlayer();
    150.                             break;
    151.  
    152.                         }
    153.                         else if (hitObject.GetComponent<Spawner>().tagg == CompartmentType[0]
    154.                            || hitObject.GetComponent<Spawner>().tagg == CompartmentType[1])
    155.                         {
    156.  
    157.                             wreckage = hitObject.GetComponent<Spawner>().SP;
    158.  
    159.                             foreach (GameObject w in wreckage)
    160.                             {
    161.                                 w.SetActive(false);
    162.                                 if (w.name == CompartmentType[2])
    163.                                 {
    164.                                     w.SetActive(true);
    165.                                 }
    166.  
    167.                             }
    168.                             hitObject.GetComponent<Spawner>().tagg = CompartmentType[2];
    169.                             tagged = CompartmentType[2];
    170.                             player.tags[i] = tagged;
    171.                             player.SavePlayer();
    172.                             break;
    173.                         }
    174.  
    175.                         else
    176.                         {
    177.                             hitObject = null;
    178.                         }
    179.                     }
    180.  
    181.                                     }
    182.                                 }
    183.                             }
    184.                         }
    185.                  
    186.  
    187.     public void PopulateCompartments()
    188.     {
    189.  
    190.         // Populates the compartments according to the Player script loaded from file Game.Dat;
    191.  
    192.         for (int i = 0; i < player.tags.Length; i++)
    193.         {
    194.  
    195.             tagged = player.tags[i];
    196.  
    197.        
    198.             if (tagged == CompartmentType[5])
    199.                 {
    200.    
    201.                 laboratory = spawners[i].GetComponent<Spawner>().SP;
    202.              
    203.                 foreach (GameObject l in laboratory)
    204.                 {
    205.                     l.SetActive(false);
    206.                     if (l.name == CompartmentType[5])
    207.                     { l.SetActive(true);}
    208.  
    209.                 }
    210.            
    211.             }
    212.             else if (tagged == CompartmentType[4])
    213.             {
    214.  
    215.                 engineering = spawners[i].GetComponent<Spawner>().SP;
    216.  
    217.                 foreach (GameObject e in engineering)
    218.                 {
    219.                     e.SetActive(false);
    220.                     if (e.name == CompartmentType[4])
    221.                     {
    222.                         e.SetActive(true);
    223.                     }
    224.  
    225.                 }
    226.          
    227.             }
    228.  
    229.             else if (tagged == CompartmentType[3])
    230.             {
    231.  
    232.                 barracks = spawners[i].GetComponent<Spawner>().SP;
    233.  
    234.                 foreach (GameObject b in barracks)
    235.                 {
    236.                     b.SetActive(false);
    237.                     if (b.name == CompartmentType[3])
    238.                     {
    239.                         b.SetActive(true);
    240.                     }
    241.  
    242.                 }
    243.          
    244.             }
    245.             else if (tagged == CompartmentType[2])
    246.             {
    247.    
    248.                 cleared = spawners[i].GetComponent<Spawner>().SP;
    249.  
    250.                 foreach (GameObject c in cleared)
    251.                 {
    252.                     c.SetActive(false);
    253.                     if (c.name == CompartmentType[2])
    254.                     {
    255.                         c.SetActive(true);
    256.                     }
    257.  
    258.                 }
    259.      
    260.             }
    261.  
    262.             else if (tagged == CompartmentType[0] || tagged == CompartmentType[1])
    263.             {
    264.    
    265.                 wreckage = spawners[i].GetComponent<Spawner>().SP;
    266.  
    267.                 foreach (GameObject w in wreckage)
    268.                 {
    269.                     w.SetActive(false);
    270.                     if (w.name == CompartmentType[1])
    271.                     {
    272.                         w.SetActive(true);
    273.                     }
    274.                  
    275.                 }
    276.  
    277.  
    278.             }
    279.             else
    280.             {
    281.                 wreckage = spawners[i].GetComponent<Spawner>().SP;
    282.  
    283.                 foreach (GameObject w in wreckage)
    284.                 {
    285.                     w.SetActive(false);
    286.                     if (w.name == CompartmentType[0])
    287.                     {
    288.                         w.SetActive(true);
    289.                     }
    290.  
    291.                 }
    292.                 tagged = CompartmentType[0];
    293.             }
    294.          
    295.  
    296.         }
    297.         }
    298.  
    299.     }
    300.  
    301.  
     
    Last edited: Dec 17, 2020
  2. Spares

    Spares

    Joined:
    Apr 19, 2019
    Posts:
    3
    Update, seems it was the tags issue where I had some obsolete tags in the editor. When I deleted them, the errors in my scripts started appearing in the console window. So the build was accurate while the editor was for some reason bug free.

    At least now I can debug it.