简体   繁体   English

在Unity中使用第三方软件包

[英]Using third party packages in Unity

I'm using UFPS in my FPS game. 我在FPS游戏中使用UFPS。 UFPS has a class vp_DamageHandler. UFPS具有类vp_DamageHandler。 It's attached to my target. 它附在我的目标上。 Inside this class, there's: 在本课程中,有:

public virtual void Die()
{
dieFunctionCalled = true; //the only thing I've added to this class, besides declaring dieFunctionCalled
...some UFPS code...//among other things, this is where the object is destroyed or deactivated
}

I want to keep the original files as clean as possible. 我想保持原始文件尽可能干净。 I want to do my own coding in a different script to keep things tidy. 我想在不同的脚本中进行自己的编码,以保持代码整洁。

I want to do some other things when the target dies, so I've added myScript to my target as well, and tried this: 当目标死亡时,我还想做其他事情,所以我也将myScript添加到了目标中,并尝试了以下方法:

void Update () {
        if (GetComponent<vp_DamageHandler> ().dieFunctionCalled) {
            GetComponent<vp_DamageHandler> ().dieFunctionCalled = false;
            Debug.Log ("dieFunctionCalled");
        }
}

The problem is that the target is destroyed before the dieFunctionCalled is set to true. 问题在于,在dieFunctionCalled设置为true之前,目标已销毁。 So the moment the target dies, myScript is deactivated and doesn't log dieFunctionCalled . 因此,当目标死亡时,myScript将被停用,并且不会记录dieFunctionCalled If I would respawn the target, dieFunctionCalled does show up in the console. 如果我重新生成目标, dieFunctionCalled确实会显示在控制台中。

I've tried attaching myScript to a empty GameObject, but then I get 我尝试将myScript附加到一个空的myScript ,但是随后我得到了

Object reference not set to an instance of an object 你调用的对象是空的

Anyone who can help me with this? 有人可以帮助我吗? I'm new to Unity. 我是Unity新手。 Basically, all I want is to keep the original UFPS files as clean as possible, and write all my scripts in a different file. 基本上,我要做的就是保持原始UFPS文件尽可能干净,并将所有脚本写入另一个文件中。

I've tried attaching myScript to a empty GameObject, but then I get Object reference not set to an instance of an object 我曾尝试将myScript附加到一个空的GameObject,但随后我得到的Object引用未设置为一个对象的实例

Create an empty GameObject called "DamageHandlerHolder" then Attach the script to it. 创建一个名为“ DamageHandlerHolder”的空GameObject,然后将脚本附加到该对象。

Find the Object that is holding the script 查找保存脚本的对象

GameObject oObj = GameObject.Find("DamageHandlerHolder");

Get the script attached to it 获取附带的脚本

vp_DamageHandler vpDm = oObj.GetComponent<vp_DamageHandler>();

Now you can check the variable: 现在您可以检查变量:

if(vpDm.dieFunctionCalled){}

This solves your null problem but I suggest you use EventManager to register to events and be notified the player dies. 这解决了您的null问题,但是我建议您使用EventManager注册事件并通知玩家死亡。

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

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