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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bug Why is the object not geting destroyed? (im new)

Discussion in 'Scripting' started by Lexus1337, Nov 2, 2022.

  1. Lexus1337

    Lexus1337

    Joined:
    Jul 22, 2022
    Posts:
    3
    Context: Script 1 is supposed to destroy the object spawned in Script 2. Script 2 is on a GameObject, that is referenced in Script 1 as turret.

    The idea is to get the Script 2 and then destroy the instantiated object, that was saved in _MainBaseShield. For some reason it does not work. There are no errors but when I test it it the game, the object saved in savedMainBaseShield is not geting destroyed. By entering Debug.Log I found out that the command IS called. So I dont get it, please help

    Code (CSharp):
    1. Script 1
    2. ...
    3.  
    4. Turret turretScript;
    5. ...
    6.     turretScript = turret.GetComponent<Turret>();
    7.     DestroyImmediate(turretScript.savedMainBaseShield);
    8. ...



    Code (CSharp):
    1. Script 2
    2. ...
    3. public GameObject savedMainBaseShield;
    4. ...
    5.     GameObject _MainBaseShield = (GameObject)Instantiate(MainBaseShield, transform.position + new Vector3(0, 1f, 0), transform.rotation * Quaternion.Euler (-90f, 0f, 0f));
    6.             savedMainBaseShield = _MainBaseShield;
    7.         }
    8. ...
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Please only use the Windows Platform forum if it's specifically about that. Your post is Scripting so please use the Scripting forum.

    I'll move your post.
     
    Bunny83 likes this.
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Well, first of all, you should pretty much never be using DestroyImmediate. If you read the documentation, it says to pretty much only use it in Editor code, and even then sparsely.

    Second, you should be using Debug.Log more flexibly, you can use it to print out what you're trying to Destroy, what objects are calling it, and much more. There's more to investigate here.

    Third, you should probably just post all of the code, unless it's obscenely long. At the very least the entire methods. The problem could be a lot of things and you're giving everyone else minimal information.
     
    Lexus1337 likes this.
  4. Lexus1337

    Lexus1337

    Joined:
    Jul 22, 2022
    Posts:
    3
    I solved the problem by adding the MainBaseShield directly to the prefab its supposed to be. For that reason the code above is not needed anymore since the Shield now gets destroyed with the object its assigned to. RadRedPanda, the code would have been pretty much too long. Script 1 with 240 Lines and Script 2 with 200. Nethertheless thank you