Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by FNC-Afonso · Dec 15, 2015 at 11:59 AM · c#destroystaticstatic-function

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 ();    
     }
 
 }



Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Dec 15, 2015 at 12:03 PM 0
Share

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.

avatar image Wilson28300 · Dec 15, 2015 at 12:08 PM 0
Share

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();

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FNC-Afonso · Dec 15, 2015 at 08:02 PM 0
Share

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

avatar image
5

Answer by Suhrahj · Feb 26, 2017 at 05:17 PM

Doing

 UnityEngine.Object.Destroy(obj);

works for me.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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);
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges