简体   繁体   中英

Can't find definition of Class from other script

I have two scripts:

1: My Player Script

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:

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; :

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

I believe the .asmdef file may have something to do with it but not sure. 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

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. It should look like this public static FireBaseScript instance; 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. To do that put the following code 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. To access variables from other script, just use this piece of code FireBaseScript.instance. your_variable_or_method , use this piece of code whenever you need.

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.

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