简体   繁体   English

如何在Unity C#中从静态方法销毁对象

[英]How to Destroy Object from static method in Unity C#

Destroy() method is not accessible in static method 在静态方法中无法访问Destroy()方法

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

But Destroy() is only accessible if: 但是只有在以下情况下才能访问Destroy():

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. 将Die函数设为非static函数。 Let's say that this script is named OtherScript . 假设此脚本名为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脚本被附加到与GameObject.Find函数然后使用GetComponent函数来获取OtherScript从游戏对象参考:

OtherScript otherScript;

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

You can now call the Die function with otherScript.Die() . 现在,您可以使用otherScript.Die()调用Die函数。 Note that you must replace "NameOfGameObjectOtherScriptIsAttachedTo" with the name of GameObject the OtherScript script is attached to. 请注意,您必须将“ NameOfGameObjectOtherScriptIsAttachedTo”替换为附有OtherScript脚本的OtherScript的名称。

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# 我只添加此内容是因为您的标题询问了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. 无法杀死单个static对象,它不会以这种方式工作。 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'. 静态变量表示“所有车辆共享此值中只有1个”。 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 结果是它可以使用'this'运算符(这很有意义!)并访问其实例的非静态成员变量和共享的静态成员变量

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?). 静态功能并不与单个车辆实例相关,因此“ this”运算符没有任何意义(“ this”将是什么?)。 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'. 也就是说,您期望称呼“ Die”为“请杀死这个”。 As a result it should not be static. 结果,它不应该是静态的。 You'll also need to access the 'gameObject' variable, not the 'GameObject' type.* 您还需要访问“ gameObject”变量,而不是“ GameObject”类型。*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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