小编Rod*_*ian的帖子

如何正确继承 Unity 的回调函数,如 Awake()、Start() 和 Update 和 FixedUpdate()?

我创建了一个父类,我希望它具有与测试 GameObject 是否接地、在水中、在空气中等相关的所有功能……鉴于此功能将被玩家以及其他 GameObjects 使用。但是,子类似乎没有正确继承这些功能。

父脚本如下:

public class CharacterPhysic : MonoBehaviour {
  [SerializeField] protected Transform groundPoints;
  float grounRadius;
  private void Start ()
  {
     whatIsGround = LayerMask.GetMask("Ground");
     groundRadius = 0.01f;
  }
  protected bool IsGrounded()
  {
     Collider2D[] colliders = Physics2D.OverlapCircleAll(groundPoints.position, groundRadius, whatIsGround);
     if (colliders.Length > 0)
     {
         return true;
     }
     else return false;
  }
  private void FixedUpdate()
  {
     Debug.Log(IsGrounded());
  }
}
Run Code Online (Sandbox Code Playgroud)

而儿童脚本只是:

public class ErrantMove : CharacterPhysic {
  private void FixedUpdate()
  {
     Debug.Log(IsGrounded());
  }
}
Run Code Online (Sandbox Code Playgroud)

当将第一个脚本作为组件添加到游戏对象时(在定义了 grounPoint 之后)Debug.Log(IsGrounded());返回TRUE

但是,当将第二个脚本作为组件添加到同一个游戏对象时(在定义了 …

c# inheritance function unity-game-engine

3
推荐指数
2
解决办法
5950
查看次数

标签 统计

c# ×1

function ×1

inheritance ×1

unity-game-engine ×1