从另一个脚本访问一个脚本中的方法

Rob*_*ert 1 c# unity-game-engine

我有一对我用C#编写的脚本,其中一个脚本从另一个脚本调用一个方法.但是,我收到以下错误:

Member 'PlayerActions.Attack()' cannot be accessed with an instance reference; qualify it with a type name instead

这是我想要调用的方法所在的位置:

public class PlayerActions:MonoBehaviour{
    public static void Attack(){
        Debug.Log("Attacking");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我试图调用该方法的地方:

public class Combat:MonoBehaviour{
    PlayerActions playerActions;
    void Start(){
        playerActions = GetComponent<PlayerActions>();
        playerActions.Attack();
    }
}
Run Code Online (Sandbox Code Playgroud)

两个脚本都附加到同一个游戏对象.

任何人都可以告诉我如何修复上面提到的错误以及为什么我实际上得到错误?我一直认为你需要引用你试图访问的类,但从我的理解,这个错误是另外说的.

Cù *_*iếu 5

删除static修饰符,您的代码将起作用!

public class PlayerActions:MonoBehaviour{
    public void Attack(){
        Debug.Log("Attacking");
    }
}
Run Code Online (Sandbox Code Playgroud)