简体   繁体   English

无法从其他脚本中找到 Class 的定义

[英]Can't find definition of Class from other script

I have two scripts:我有两个脚本:

1: My Player Script 1:我的播放器脚本

using Unity.FPS.Game;
using UnityEngine;
using UnityEngine.Events;
using Spells;

namespace Unity.FPS.Gameplay
{
    public class PlayerCharacterController : MonoBehaviour
    {

        FireBaseScript fireScript;
        ...
    }
}

2: My Fire Spell Script: 2:我的火咒脚本:

using UnityEngine;
using System.Collections;

namespace Spells
{
    public class FireBaseScript : MonoBehaviour
    {
        ...
    }
}

Both scripts are in different folders and I get an error in the Player script on the line using Spells;两个脚本都在不同的文件夹中,我在using Spells; :

The type or namespace 'Spells' could not be found (are you missing a directive or assembly reference

Is there something I need to do for my Player script to recognize my fire spell script?我需要为我的播放器脚本做些什么来识别我的火咒脚本吗?

EDIT: --------------------------编辑: - - - - - - - - - - - - -

So I tried copying the FireBaseScript.cs into the same folder as the Player script and it compiled just fine.所以我尝试将FireBaseScript.cs复制到与 Player 脚本相同的文件夹中,它编译得很好。 I then did some more googling and discovered that unity folder are compiled in waves and not all together.然后我做了一些谷歌搜索,发现统一文件夹是按波编译的,而不是一起编译的。 So apparently the /Scripts/Games/ folder is compiled before the /Scripts/Spells/Animations/Fire folder.所以显然/Scripts/Games/文件夹是在/Scripts/Spells/Animations/Fire文件夹之前编译的。

I believe the .asmdef file may have something to do with it but not sure.我相信.asmdef文件可能与它有关,但不确定。 I tried adding an assembly definition file to the Spells folder and adding it to the Assembly Definition References of the Games.asmdef file - however that still didn't work我尝试将程序集定义文件添加到Spells文件夹并将其添加到Games.asmdef文件的Assembly Definition References中-但这仍然不起作用

Is there a way to get these two folders to be compiled together?有没有办法让这两个文件夹一起编译?

In you FireSpell Script, create an instance of it using your class name.在您的 FireSpell 脚本中,使用您的 class 名称创建它的一个实例。 It should look like this public static FireBaseScript instance;它应该看起来像这个公共 static FireBaseScript 实例; and put this piece of code where you used to create local variables.并将这段代码放在你用来创建局部变量的地方。 Then in that same Script at Awake() or at Start() method, make sure that only one instance of this is available.然后在同一脚本中的 Awake() 或 Start() 方法中,确保只有一个可用的实例。 To do that put the following code if(instance == null) {instance = this;} else if(instance;= this) { Destroy(gameObject);为此,请输入以下代码if(instance == null) {instance = this;} else if(instance;= this) { Destroy(gameObject); } . } After this you can use this instance in any other Script inside Unity to access variables or functions.在此之后,您可以在 Unity 内的任何其他脚本中使用此实例来访问变量或函数。 To access variables from other script, just use this piece of code FireBaseScript.instance.要从其他脚本访问变量,只需使用这段代码FireBaseScript.instance。 your_variable_or_method , use this piece of code whenever you need. your_variable_or_method ,在需要时使用这段代码。

Please make sure that you replace your_variable_or_method with the variable or method that you created as public variable or method in FireBaseScript.请确保将your_variable_or_method替换为您在 FireBaseScript 中作为公共变量或方法创建的变量或方法。

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

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