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. Dismiss Notice

[SOLVED] Destroy(GetComponent(this))

Discussion in 'Scripting' started by Deleted User, Oct 29, 2014.

  1. Deleted User

    Deleted User

    Guest

    I'm trying that when a script's timer reaches 0, remove the component.
    This is what I have:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BuffScript : MonoBehaviour
    5. {
    6.         public float duration;
    7.         public float durationRemaining;
    8.        
    9.         protected internal HealthScript health;
    10.        
    11.         protected virtual void Awake ()
    12.         {
    13.                 health = GetComponent<HealthScript> ();
    14.         }
    15.  
    16.         void Update ()
    17.         {
    18.                 if (durationRemaining > 0) {
    19.                         durationRemaining -= Time.deltaTime;
    20.                 } else {
    21.                         RemoveBuff (this);
    22.                 }
    23.         }
    24.        
    25.         public void RemoveBuff (string buff)
    26.         {
    27.                
    28.                 Destroy (GetComponent (buff));
    29.         }
    30.        
    31.         public void RemoveAllBuffs ()
    32.         {
    33.                 BuffScript[] buffs = GetComponents<BuffScript> ();
    34.                 foreach (BuffScript buff in buffs) {
    35.                         Destroy (buff);
    36.                 }
    37.         }
    38. }
     
  2. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    What error is it crapping out?
     
  3. Deleted User

    Deleted User

    Guest

    this refers to the GameObject the script is attached to and not the script itself.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Mrmmm... nope.

    'this' refers to the object inwhich the method has scope (if there is an object... static methods have no object to be scoped to, thusly a compiler error will occur).
    http://msdn.microsoft.com/en-us/library/dk1507sz.aspx

    If in a 'MonoBehaviour', 'this' will be your script.

    this.gameObject is the GameObject.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377

    You say:
    Code (csharp):
    1.  
    2. RemoveBuff(this);
    3.  
    But RemoveBuff expects a string, and that string is used to GetComponent.

    But 'this' is a component already, you should probably be getting a compiler error like:

    Or something like:

    Or both.

    You should do something like:

    Code (csharp):
    1.  
    2.         void Update ()
    3.         {
    4.                 if (durationRemaining > 0) {
    5.                         durationRemaining -= Time.deltaTime;
    6.                 } else {
    7.                         Destroy(this);
    8.                 }
    9.         }
    10.  
    Or even have some overloads of RemoveBuff like so:

    Code (csharp):
    1.  
    2. //removes this buff
    3. public void RemoveBuff()
    4. {
    5.     Destroy(this);
    6. }
    7.  
    8. //removes a buff if it's on the same gameobject as this
    9. public void RemoveBuff(BuffScript buff)
    10. {
    11.     if(buff.gameObject == this.gameObject) Destroy(buff);
    12. }
    13.  
     
    Deleted User and ThermalFusion like this.
  6. Deleted User

    Deleted User

    Guest

    @lordofduct, Interesting because in my original code I have "this.name" and when I print it, it says the monsters name and not the script and nothing is destroyed.
     
  7. axenlader

    axenlader

    Joined:
    Feb 18, 2014
    Posts:
    6
    Hi, I think that instead of removing the component, you better (don't know if it is really better) desactivate it
    GetComponent<ComponentName>().enabled =true;
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    That's because this.name uses the inherited Object.name property. lordofduct's post is correct.

    --Eric
     
    Deleted User likes this.
  9. Deleted User

    Deleted User

    Guest

    That explains it.
    Thanks guys.