Search Unity

Destroying Subclass that inherits MonoBehaviour

Discussion in 'Scripting' started by CheekyLewb, Apr 25, 2015.

  1. CheekyLewb

    CheekyLewb

    Joined:
    Apr 25, 2015
    Posts:
    3
    Hello I tried asking this question elsewhere but couldn't really get an accurate answer so I'm hoping maybe here i can find some more help, I have this code:
    Code (CSharp):
    1. public abstract class Test : MonoBehaviour
    2. {
    3.     public abstract void OnStart();
    4.     public abstract void OnUpdate();
    5.  
    6.     private void Start()
    7.     {
    8.         OnStart();
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.X))
    14.         {
    15.             Destroy(this);
    16.         }
    17.         OnUpdate()
    18.     }
    19. }
    Then I have this :
    Code (CSharp):
    1. public class SecondTest : Test
    2. {
    3.     public override void OnStart()
    4.     {
    5.  
    6.     }
    7.  
    8.     public override void OnUpdate()
    9.     {
    10.         Writter.Log("Running");
    11.     }
    12. }

    However whenever I press X, my writter keeps logging, it seems like the instance of "Test" gets destroyed but not my "SecondTest" is there any way to solve this? thank you!
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Yes. Destroy SecondTest.
     
  3. CheekyLewb

    CheekyLewb

    Joined:
    Apr 25, 2015
    Posts:
    3
    I'm trying to prevent from having Destroy inside SecondTest, because there will be a lot of classes like it and I would like for all of them to be destroyed on keypress, is there no other way of doing this other than adding a check on each sencdtest class?
     
  4. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    SecondTest.cs inherits from Test.cs. You cannot destroy an instance of Test.cs and assume it will also destroy SecondTest.cs. You have to destroy the script that you want to destroy. Even another instance of Test.cs will not be destroyed if you only destroyed a certain instance of it.
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    You would want to get all instances of the class you are trying to destroy and destroy them one by one by iterating through an array or a list
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm puzzled as to why this wouldn't work. Does a debug.log in you base class (outside the if) show up? What about one inside the if?

    @outwarddesign in the nicest possible way you don't know what you are talking about. The derived class is the base class. It's basic polymorphism. Go google it.
     
  7. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    So, if I have two instances of SecondTest.cs and I destroy one instance of that class, because of polymorphism the other one is destroyed as well?
     
  8. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I guess I misunderstood the question :p
    You can make a method in the Test.cs class and call that on SecondTest.cs, but for each instance of SecondTest.cs you will have to call that method on it.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    So this one was bothering me. As written it should work. @outwarddesign and I had some discussion off thread, and both of us think the same thing.

    So I actually fired up Unity and put in your scripts as written (+ a semicolon on line 17 of Test :), I also used Debug.Log instead of your Writer.). I attached SecondTest to a GameObject in an empty scene.

    And what do you know, this works perfectly as written. Hit play and I get a stream of debugs. Hit X and the component disappears. No more debugs.

    So whatever your problem is, its not in the scripts shown here.
     
    CheekyLewb likes this.
  10. CheekyLewb

    CheekyLewb

    Joined:
    Apr 25, 2015
    Posts:
    3
    Thanks a lot for your help!