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

[Help] How to destroy a gameobject from a diffrent gameobject

Discussion in 'Scripting' started by YouBungy, Aug 13, 2017.

  1. YouBungy

    YouBungy

    Joined:
    Jun 19, 2017
    Posts:
    32
    Hey all! so I have this problem and I have searched all over the web for this so I don't even know if its possible. So basicly I'm trying to Destroy a gameobject from a different gameobject via script. Ill show you the fraction of my code it I'm talking about
    Code (CSharp):
    1. public GameObject CrystalForPlaceG;
    2. void Update()
    3.     {
    4.         if (canDestroy == 1)
    5.         {
    6.             if (Input.GetButtonDown("Destroy"))
    7.             {
    8.                 Inventory.text = "Crystal";
    9.                 //BuildingCrystal.SetActive(false);
    10.                 //CrystalForPlaceG.SetActive(false);
    11.                 //Object.Destroy(CrystalForPlaceG);
    12.                 CanPlace = 1;
    13.                 canDestroy = 0;
    14.             }
    15.         }
    Ignore the fact that it looks like there are parts missing this is like a tiny fraction of my code and I have a ton more variables. Anyways what I'm trying to get to happen is that when you Destroy a block it Destroys the gameobject. but the problem here is I'm doing it from another script so when I play it says deleting unity assets is not permitted due to data loss. as you can see I tried multiple methods and none worked. for the set active it wouldent set the gameobject to false and then when I place another crystal it sets it false even though when I am pressing the place key (this is also in update) it sets it active but for some reason its overriding it I guess. If anyone could help that would be great. Thanks!
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Is your CrystalForPlaceG a GameObject on scene or a reference of something in your project?

    This error usually means that you are trying to destroy a prefab from your project folder and that is not permitted.
     
  3. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    You could have the object(s) you are trying to destroy subscribe to an event managed by the script destroying the remote game object. This code hasn't been tested.

    Code (CSharp):
    1.  
    2. //
    3. // Destroyer Script
    4. //
    5. public Eventhandler<EventArgs> DestroyObject;
    6.  
    7. private void OnDestroyObject()
    8. {
    9.     if (DestroyObject != null)
    10.     {
    11.         DestroyObject(this, EventArgs.Empty);
    12.     }
    13. }
    14.  
    Code (CSharp):
    1.  
    2. //
    3. // Object to destroy script
    4. //
    5.  
    6. [SerializeField]
    7. private Destroyer _destroyer;
    8.  
    9. private void OnEnable()
    10. {
    11.     _destroyer.DestroyObject += OnDestroyObject;
    12. }
    13.  
    14. private void OnDisable()
    15. {
    16.     _destroyer.DestroyObject -= OnDestroyObject;
    17. }
    18.  
    19. private void OnDestroyObject(object sender, EventArgs e)
    20. {
    21.     Destroy(gameObject);
    22. }
    23.  
     
  4. YouBungy

    YouBungy

    Joined:
    Jun 19, 2017
    Posts:
    32
    It's a prefab but it's also in my scene
     
  5. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Then that's your issue. You are trying to destroy the prefab reference instead of the instantiated GameObject. You need to call the destroy and pass the instantiated GameObject reference, not the prefab one.
     
    dpgdemos likes this.