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: https://www.youtube.com/watch?v=-igoV67B5h8 (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 ();
}
}
I would recommend reading up on what "static" means.
Your "Die" function almost certainly shouldn't be static (because it looks like something you should be doing to a specific instance). If you remove the "static", you should find that it lets you put gameObject
(the current instance's GameObject) on line 10 ins$$anonymous$$d of GameObject
(the class name).
Line 26 will also need changing to enemy.Die()
. Line 26 in fact is probably the one that ought to have rung warning bells. You're trying to make a specific Enemy die at that point, right? But the line doesn't refer to a specific Enemy so how is the code supposed to know which one to make die? This, essentially, is why Die
needs to be non-static.
The tutorial shows javascript but this is c#. Try without static. And make it: Destroy(gameObject) and not Destroy(GameObject) ; Also, in DieOnHit class, it should be enemy.Die();
Answer by wibble82 · Dec 15, 2015 at 12:13 PM
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
Thank you I am a bit noob just started and I was thinking that you needed it to be static so it can be accessed by other scripts... :p
Answer by tanoshimi · Dec 15, 2015 at 12:01 PM
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);
}
Your answer
Follow this Question
Related Questions
How remove/delete/destroy an element from an array by value in c#? 2 Answers
Remove Door's collider if cube is in the trigger,Destroy Door's Collider if box is in the trigger 0 Answers
Switch-Case Statements and stateInfo.fullPathHash 1 Answer
When should I use static fields/methods? 2 Answers
Instantiate an object and destroy it after given time 2 Answers