Unity Error CS1061

时间:2016-01-03 13:35:00

标签: c# unity3d

好的,我从Unity引擎收到此错误,我不确定如何修复它。我一直试图找到解决方案,但我什么都没有。我是脚本编写的初学者,因为我以前从未编写脚本。我的脚本看起来像这样:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerBullet : MonoBehaviour
{
    public float speed;

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        var movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        var rigidbody = GetComponent<Rigidbody> ();
        rigidbody.velocity = movement * speed;

        object boundary = null;
        rigidbody.position = new Vector3
            (
             Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
            );

    }
}

我可以帮忙解决一下如何解决这个问题吗?它说我的xMin,xMax,zMin和zMax存在问题。这应该是一个子弹地狱,并防止玩家走出界限,但如果我不能设置边界,它就无法工作。 我正在学习本教程:https://www.youtube.com/watch?v=zJHRkdRq4BQ (这都是用C#编写的)

如果有任何帮助,我是否可以获得另一个好的教程?

1 个答案:

答案 0 :(得分:2)

来自the documentation

  

&#39;类型&#39;不包含&#39;成员&#39;的定义没有延伸   方法&#39;名称&#39;接受类型&#39;类型&#39;的第一个参数。可以找到   (您是否缺少using指令或程序集引用?)。这个   尝试调用方法或访问类成员时发生错误   那不存在。

你的问题在这里:

object boundary = null;
rigidbody.position = new Vector3
    (
        Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
        0.0f,
        Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );

问题是boundary的类型为object,而object不包含xMin的定义,也不包含xMax等的定义为了实现这一点,boundary必须是Boundary类型,如下所示:

Boundary boundary = null;
rigidbody.position = new Vector3
    (
        Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
        0.0f,
        Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );

然而,这将为您提供NullReferenceException,因为您正在尝试使用未创建的对象中的属性。你可以做这样的事情来解决这个问题:

Boundary boundary = new Boundary
{
    xMin = 134.0f;
    xMax = 146.0f;
    zMin = 112.0f;
    zMax = 153.0f;
}; // assign some values otherwise it will all default to 0.0f
rigidbody.position = new Vector3
    (
        Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
        0.0f,
        Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );