簡體   English   中英

從Unity C#中的其他腳本訪問布爾

[英]Accessing a bool from a different script in Unity C#

我的玩家的運動控制腳本中有一個bool變量( isGrounded ),我想在另一個GameObject訪問它。

BallController.cs

public class BallController : MonoBehaviour {
    Transform myTrans;
    Rigidbody2D myBody;

    public bool isGrounded = true;
    public bool release = false;
}

GravityPull.cs

public class GravityPull : MonoBehaviour {

    public GameObject target;
    public int moveSpeed;
    public int maxdistance;
    private float distance;


    void Start ()
    {
        target= (GameObject.Find("Ball (1)"));
    }


    void Update ()
    {
        distance = Vector2.Distance (target.transform.position, transform.position);

        if (distance < maxdistance && target.isGrounded)
        {
             target.transform.position = Vector2.MoveTowards(target.transform.position, transform.position, moveSpeed * Time.deltaTime / distance);
        }
    }
}

如果我做我的目標一個GameObject不是我可以用它找到.find 但是,如果執行此操作,則無法訪問布爾值。 如果我將目標BallController則可以訪問bool,但是無法使用.find查找類。 我也無法施展的GameObjectBallController 有人可以告訴我我在做什么錯嗎?

target.getComponent<BallController>().isGrounded

這應該足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM