简体   繁体   English

引用 Unity 中的脚本

[英]Reference to a script in Unity

This is a really dumb question but I have been trying to reference to the DisableMovement method by creating object, and why the player continue moving after can move is false?这是一个非常愚蠢的问题,但我一直试图通过创建对象来引用 DisableMovement 方法,为什么玩家在可以移动之后继续移动是错误的? (the player touches the ground ) (玩家触地)

// PlayerController script using UnityEngine; // 使用 UnityEngine 的 PlayerController 脚本;

public class PlayerController : MonoBehaviour
{
    
     bool canMove;
    // Start is called before the first frame update
    void Start()
    {
        canMove = true;
        
    }

    // Update is called once per frame
    void Update()
    {
        if (canMove)
        {
            RotatePlayer();
            BoostUp();
        }
    }

    private void BoostUp()
    {
        
    }

    private void RotatePlayer()
    {
        
    }
    public void DisableMovement()
    {
        canMove = false;
    }
}

// CrashDetector script // 崩溃检测器脚本

using UnityEngine;
using UnityEngine.SceneManagement;

public class CrashDetector : MonoBehaviour
{
    
    PlayerController playerController = new PlayerController();

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
           
            playerController.DisableMovement();
            Invoke("ReloadScene", FinishLine.DelayTime - 3);
        }
    }
    void ReloadScene()
    {

        SceneManager.LoadScene("Level 1");
    }
}

And lastly, What is the purpose of creating OOP object in Unity script?最后,在 Unity 脚本中创建 OOP 对象的目的是什么? I saw that we could access the script through get component but is there any solution else to access it by creating object or use static keyword?我看到我们可以通过 get 组件访问脚本,但是还有其他解决方案可以通过创建对象或使用 static 关键字来访问它吗? And when to use static keyword?什么时候使用静态关键字?

PlayerController playerController = new PlayerController();

is not allowed and makes no sense.是不允许的,也没有任何意义。

You do not want to create a new instance of PlayerController - which again is not allowed by Unity anyway: Using new on a MonoBehaviour will print a warning and lead to unexpected behavior!您不想创建PlayerController的新实例 - Unity 也不允许这样做:在MonoBehaviour上使用new将打印警告并导致意外行为!

What you rather want to do is getting a reference to an already existing instance eg have a field exposed in he Inspector您宁愿做的是获取对现有实例的引用,例如在 Inspector 中公开一个字段

public PlayerController playerController;

or或者

[SerializeField] private PlayerController playerController;

and reference your existing component via drag and drop.并通过拖放引用您现有的组件。

Or altaneratively get in on runtime via eg或者通过例如替代地进入运行时

private PlayerController playerController;

private void Awake()
{
    // if it is attached to the same object
    playerController = GetComponent<PlayerController>();

    // if it is somewhere higher in the hierarchy
    playerController = GetComponentInParent<PlayerController>();

    // if it is somewhere lower in the hierarchy
    playerController = GetComponentInChildren<PlayerController>();

    // if there is only one but anywhere in the scene
    playerController = FindObjectOfType<PlayerController>();
}

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

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