Can't destroy GameObject in static method

Error CS0118: ‘UnityEngine.GameObject’ is a ‘type’ but is used like a ‘variable’ (CS0118) (Assembly-CSharp)
Tutorial video in javascript: 18. Unity Tutorial, ENEMIES - MAKE A GAME - YouTube (21:30)
Hello I have been following Brackeys tutorial and I’m stuck with a bug I can’t find nowhere. Here’s the code and my tries:

//Assigning a variable to the gameobject but doing a static one would mess up other enemys I think
 
public static void Die ()
 
{
 
Object.Destroy (GameObject); //Tried: "Destroy (GameObject);"    "Destroy (this.GameObject);"
 
}

Also tried:
Destroy (gameObject);
Error CS0120: An object reference is required for the non-static field, method, or property ‘UnityEngine.Component.gameObject.get’

FULL SCRIPTS:

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour 
{
	

	public static void Die () 
	{
		Destroy (GameObject);
	}
		

}


using UnityEngine;
using System.Collections;

public class DieOnHit : MonoBehaviour {

	void OnTriggerEnter () 
	{
		Enemy enemy = transform.parent.GetComponentInParent<Enemy>();
		Enemy.Die ();	
	}

}

Doing

UnityEngine.Object.Destroy(obj);

works for me.

You don’t want that method to be static. And you want to destroy the instance of the gameobject (gameObject), not the GameObject class (GameObject):

 public void Die () 
 {
     Destroy (gameObject);
 }

I think perhaps you’ve misunderstood the ‘static’ keyword a little bit.

To clarify, a bit… Imagine you have a class called ‘Vehicle’.

  • A none-static variable means ‘every vehicle has its own copy of this variable’. We might say 'every instance of vehicle has its own copy of the variable.
  • A static variable means ‘there is only 1 of this value shared by all vehicles’. Here we’d say 'all instances of vehicle share the variable.

Following on from that, functions are a little harder to picture, but they work in much the same way:

  • A none-static function operates on an instance of the vehicle. The result is that it can use the ‘this’ operator (it makes sense!) and access both none-static member variables of it’s instance, and the shared static ones
  • A static function isn’t tied to an individual instance of a vehicle, so the ‘this’ operator doesn’t make any sense (what would ‘this’ be?). It still makes sense for it to be able to access static variables, but again none-static ones don’t make any sense - who’s version of the variable would it be referring to?

Your ‘Die’ function looks like it is designed to operate on a given instance of your enemy. i.e. you are expecting calling ‘Die’ to mean ‘kill this please’. As a result it should not be static. You’ll also need to access the ‘gameObject’ variable, not the ‘GameObject’ type.

public class Enemy : MonoBehaviour 
 {   
     public void Die () 
     {
         Destroy (gameObject);
     }
  }

I also just noticed your call of ‘die’ should be:

void OnTriggerEnter () 
     {
         Enemy enemy = transform.parent.GetComponentInParent<Enemy>();
         enemy.Die ();    
     }

Because you calling the ‘Die’ function on the ‘instance’ of the Enemy type that the local variable ‘enemy’ refers to.

-Chris