简体   繁体   中英

How to Destroy Object from static method in Unity C#

Destroy() method is not accessible in static method

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

But Destroy() is only accessible if:

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

You can't call a non static function from a static function but you can do the opposite.

I need to make it accessible on another scripts

Make the Die function to be a non static function. Let's say that this script is named OtherScript .

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

Then from another script, you can access it by finding the GameObject the OtherScript script is attached to with the GameObject.Find function then use the GetComponent function to get the OtherScript reference from the GameObject:

OtherScript otherScript;

void Awake()
{
    GameObject obj = GameObject.Find("NameOfGameObjectOtherScriptIsAttachedTo");
    otherScript = obj.GetComponent<OtherScript>();
}

You can now call the Die function with otherScript.Die() . Note that you must replace "NameOfGameObjectOtherScriptIsAttachedTo" with the name of GameObject the OtherScript script is attached to.

From your comments it looks more like you actually want to do what Programmer's answer shows.

I'm just adding this because your title asks How to Destroy Object from static method in Unity C#


If you really need it to be static (eg in a static class) you could use it like this

using UnityEngine;

public static class SomeStaticClass
{
    public static void Die(GameObject obj)
    {
        Object.Destroy(obj);
    }
}

but to be honest this is needed in very few cases. It might be helpful eg in an Editor script where you don't have any Component executing your code.

cannot kill a single static object, it dosnt work that way. please refer to the answer here.

the following excerpt is from the above link, and should explain for you...

*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. ie 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.*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM