Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Destroying gameobject with tag not working correctly in build, but works in Editor

Discussion in 'Scripting' started by seconddaytv, Feb 28, 2021.

  1. seconddaytv

    seconddaytv

    Joined:
    Jan 22, 2020
    Posts:
    6
    Hello everybody,
    I am struggleing with an issue for two days now and simply can't find a way to fix it. This issue only appears in the windows build of my project, not when I test it in the editor.

    This is a script for a basic vehicle selection. It is supposed to delete the currently displayed vehicle prefab on the click of a button (left or right) to make space for the next vehicle in the list.
    In the editor, this works without an issue, but in the build it seems to only work after I pressed each button twice (so I have to press Left-Right-Left to delete the vehicle which was displayed before I initially pressed Left)


    Code (CSharp):
    1. public class awakeManager : MonoBehaviour {
    2.  
    3.     public GameObject toRotate;
    4.     public GameObject currentCar;
    5.  
    6.     public vehicleList listOfVehicles;
    7.     public AudioSource engineAudio;
    8.  
    9.     public int vehiclePointer = 0;
    10.  
    11.     public float rotateSpeed;
    12.  
    13.     void Awake() {
    14.         //vehiclePointer = PlayerPrefs.GetInt("pointer");
    15.  
    16.         GameObject childObject = Instantiate(listOfVehicles.vehicles[vehiclePointer], Vector3.zero, Quaternion.identity) as GameObject;
    17.         childObject.transform.parent = toRotate.transform;
    18.         engineAudio = childObject.GetComponentInChildren<AudioSource>();
    19.         EngineSoundManager(engineAudio);
    20.     }
    21.  
    22.     void FixedUpdate() {
    23.         toRotate.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
    24.     }
    25.  
    26.     public void rightButton() {
    27.         if(vehiclePointer < listOfVehicles.vehicles.Length - 1) {
    28.             Destroy(GameObject.FindWithTag("Player"));
    29.             vehiclePointer++;
    30.             PlayerPrefs.SetInt("pointer", vehiclePointer);
    31.             GameObject childObject = Instantiate(listOfVehicles.vehicles[vehiclePointer], Vector3.zero, Quaternion.identity) as GameObject;
    32.             childObject.transform.parent = toRotate.transform;
    33.             engineAudio = childObject.GetComponentInChildren<AudioSource>();
    34.             EngineSoundManager(engineAudio);
    35.         }
    36.     }
    leftButton looks exactly the same, except it's

    if (vehiclePointer > 0) {...}

    Any help would be highly appreciated, cause I have absolutely no idea what could mess up the build version here. If any additional information is needed, I'll try to add it as quickly as possible!

    Thank you very much in advance!

    Edit: So of course, after struggleing with this for so long, I instantly found the reason for the issue after I created this post... :D
    The prefab of one of my cars simply had a child object with the "Player" Tag aswell, which caused the system to delete the subcomponent first...
     
    Last edited: Mar 1, 2021
    mopthrow likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,145
    The system works!
     
    seconddaytv likes this.
unityunity