我有一个isGrounded
来自我的播放器移动控制脚本的bool变量(),我想在另一个中访问它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
.但是,如果我这样做,我无法访问布尔.如果我将目标设为a,BallController
那么我可以访问bool,但我不能.find
用来查找该类.我也不能把它GameObject
作为一个BallController
.谁能告诉我这里我做错了什么?
target.getComponent().isGrounded
这应该足够了.