【问题标题】:I cannot access a component from its own gameobject (unity3d)我无法从自己的游戏对象(unity3d)访问组件
【发布时间】:2018-12-29 19:27:47
【问题描述】:

我想从 CarAgent 组件(Taxi 游戏对象)访问 Horizo​​ntalAxis 变量。 当我尝试从另一个游戏对象访问它时它工作正常,但是当我尝试在 CarUserControl 中访问它时,它也是一个出租车组件,它说 CarAgent 不存在。

这是另一个游戏对象的脚本,它工作正常:

private float HorizontalAxis;

public void Start() {

     HorizontalAxis = GameObject.Find("Taxi").GetComponent<CarAgent>().HorizontalAxis;
}

// Update is called once per frame
public void Update()
{

    transform.rotation = new Quaternion(0, 0, HorizontalAxis, 360);

}

这是 CarUserControl 脚本:

 private void Start()
    {
        HorizontalAxis = GameObject.Find("Taxi").GetComponent<CarAgent>().HorizontalAxis;
}

如何访问 CarUserControl 中的 Horizo​​ntalAxis 变量?

编辑:我尝试访问此脚本中的其他类,但它也不起作用。 这个脚本是从UnityStandardAssets/Vehicules/Car得到的,所以一开始是这样写的:

namespace UnityStandardAssets.Vehicles.Car
{
[RequireComponent(typeof (CarController))]
public class CarUserControl : MonoBehaviour
{

我是 Unity 和 c# 的新手,所以它改变了一些东西。如果是,我该如何解决?

【问题讨论】:

  • 第一个有效,因为您在获取组件之前获取了对象:GameObject.Find("Taxi")。第二个代码 sn-p 尝试从同一个 GameObject 中获取 CarAgent,而不是先执行 GameObject.Find("Taxi")
  • @Maakep 完全正确......但这应该可以工作......这就是 OP 在这里询问的原因......
  • 你是否 100% 确定这两个组件都在同一个根级别 GameObject 上并且没有以某种方式嵌套?
  • @derHugo 是的,但唯一的答案是 OP 就像你在这里写的^,试图在没有游戏对象的游戏对象上获取组件。可能是 OP 不小心将此脚本添加到另一个 GameObject 上,同时认为它是正确的故障。我建议在层次结构中搜索类名,这将突出显示所有带有脚本的对象。或者,如果动态添加,则在 CarUserControl 的 Start() 中添加 Debug.Log(gameObject.name); 以验证哪些对象正在使用它。

标签: c# unity3d unity-components


【解决方案1】:

最后,问题在于 CarUserControl 位于标准资产文件夹中,而 CarAgent 脚本不在该文件夹中。显然,标准资产文件夹不会与其他文件夹同时编译。查看更多here

【讨论】:

    【解决方案2】:

    可能是你在游戏对象链中的位置太低或太高。

    试试

    car = GetComponent<CarAgent>(this.parent);
    

    car = GetComponent<CarAgent>(this.child);
    h = car.HorizontalAxis;
    

    【讨论】:

    • 你能解释一下括号里的内容吗?
    • "this" 是指当前的游戏对象,在您的情况下是 CarUserControl
    • 是的,但是当你把它放在括号里时它在做什么呢?
    • 您正在将当前游戏对象的父或子传递给 GetComponent 方法。
    • 它返回类型UnityStandardAssets.Vehicles.Car.CarUserControl' does not contain a definition for parent'并且找不到扩展方法parent' of type UnityStandardAssets.Vehicles.Car.CarUserControl'。您是否缺少程序集参考?
    【解决方案3】:

    这主要是因为Update()(每一帧)和FixedUpdate()(每一物理帧)之间存在不一致。实际上,对于同一个 Update() 框架,可以多次调用 FixedUpdate,或者根本不调用。

    Unity 关于Order of Execution 的文档对此进行了更多解释。

    另外,在循环中查询GetComponent&lt;T&gt; 非常繁重。在Init 方法中“缓存”引用更为明智,例如Start()Awake()

    private HorizontalAxis hAxis;
    
    public void Start(){
        GameObject taxi = GameObject.Find("Taxi");
        if(taxi != null){ 
            hAxis = taxi.GetComponent<CarAgent>().HorizontalAxis;
        }
    }
    
    public void Update(){
       if(hAxis != null)
         transform.rotation = new Quaternion(0, 0, hAxis, 360);
    }
    
    public void FixedUpdate(){
        if(hAxis != null){
            // Do only Physics related stuff: rigidbodies, raycast
            // inputs and transform operations should stay in the Update()
        }
    }
    

    【讨论】:

    • 我编辑了问题,请您看一下好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多