【问题标题】:C#:Unity:Having trouble reference Rigidbody2d from different scriptC#:Unity:从不同的脚本引用 Rigidbody2d 时遇到问题
【发布时间】:2014-08-12 12:39:31
【问题描述】:

我的场景有一个主角、一个气球和一个系绳。我有一个连接到气球的弹簧接头 2d,我希望能够将连接的主体更改为玩家单击的对象。到目前为止,我有以下 2 个脚本,一个用于气球,一个用于连接体:

气球:

using UnityEngine;
using System.Collections;

public class BalloonTethering : MonoBehaviour {

    public SpringJoint2D theSpringJoint;
    public Rigidbody2D theTether;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        theSpringJoint.connectedBody = theTether;
    }
}

连接体:

using UnityEngine;
using System.Collections;

public class TetherAny : MonoBehaviour {

    public GameObject mainBalloon;
    public Rigidbody2D iAmATether = new Rigidbody2D();

    // Use this for initialization
    void Start () {
        mainBalloon.GetComponents<BalloonTethering>();
        iAmATether = this.gameObject.GetComponents<Rigidbody2D>();
    }

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

    }

    void OnMouseDown(){
        BalloonTethering.theTether = iAmATether;
    }
}

我在 TetherAny 脚本上不断收到以下两个错误:

(12,17):错误 CS0029:无法将类型“UnityEngine.Rigidbody2D[]”隐式转换为“UnityEngine.Rigidbody2D”

(21,34):错误 CS0120:访问非静态成员“BalloonTethering.theTether”需要对象引用

如果有人能告诉我哪里出错了,将不胜感激:)

谢谢!

【问题讨论】:

    标签: c# unity3d 2d game-physics


    【解决方案1】:

    从第一个错误行我可以看出 GetComponents() 将返回多个 Rigidbody2D 对象,而不仅仅是一个。

    http://docs.unity3d.com/ScriptReference/Component.GetComponents.html

    http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

    使用这两个链接了解实际发生的情况。

    在第二个..您正在尝试访问您没有对象的类的成员(这不是您访问其他对象成员的方式)

    http://msdn.microsoft.com/en-us/library/x9afc042.aspx

    老实说,在我看来,您应该首先学习如何编程基础知识。但这只是我的看法。

    【讨论】:

      【解决方案2】:

      (12,17):错误 CS0029:无法将类型“UnityEngine.Rigidbody2D[]”隐式转换为“UnityEngine.Rigidbody2D”

      GetComponents 返回刚体列表。您不能将整个列表分配给一个对象。您需要使用 GetComponent,而不是 GetComponents。单数。错误中 Rigidbody2D 之后的 '[]' 表示它是一个列表,而不是单个对象。

      (21,34):错误 CS0120:访问非静态成员“BalloonTethering.theTether”需要对象引用

      这是因为您从不告诉它您希望它引用什么 BalloonTethering。您需要在气球对象上使用 GetComponent()。这里还有其他问题,我很惊讶您没有收到更多错误。一旦您尝试访问这些变量中的任何一个,就会出现问题!我会修改第一个脚本,让你上路

          using UnityEngine;
      using System.Collections;
      
      public class BalloonTethering : MonoBehaviour {
      
          public SpringJoint2D theSpringJoint;
          public Rigidbody2D theTether;
      
          // Use this for initialization
          void Start () {
                theSpringJoint =  this.gameObject.GetComponent<SpringJoint2D>();
          }
      
          // Update is called once per frame
          void Update () {
      
          }
      }
      

      如果您希望在将其设置为 connectedBody 时发生任何事情,则需要将一个对象分配给 theTether

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-24
        • 2023-02-06
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多