C# 单位误差CS1061
好的,我从Unity engine收到这个错误,我不确定如何修复它。我一直在努力寻找解决办法,但我一无所获。我是脚本编写的初学者,因为我以前从未编写过脚本。我的脚本如下所示:C# 单位误差CS1061,c#,unity3d,C#,Unity3d,好的,我从Unity engine收到这个错误,我不确定如何修复它。我一直在努力寻找解决办法,但我一无所获。我是脚本编写的初学者,因为我以前从未编写过脚本。我的脚本如下所示: using UnityEngine; using System.Collections; [System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; } public class PlayerBullet
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)
);
}
}
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)
);
使用UnityEngine;
使用系统集合;
[系统可序列化]
公共阶级界限
{
公共浮点数xMin,xMax,zMin,zMax;
}
公共类PlayerBullet:单行为
{
公众浮标速度;
void FixedUpdate()
{
float moveHorizontal=Input.GetAxis(“水平”);
float moveVertical=Input.GetAxis(“垂直”);
var移动=新矢量3(水平移动,0.0f,垂直移动);
var rigidbody=GetComponent();
刚体。速度=运动*速度;
对象边界=空;
rigidbody.position=新矢量3
(
数学夹具(刚体.position.x,boundary.xMin,boundary.xMax),
0.0f,
数学夹具(刚体.position.z,boundary.zMin,boundary.zMax)
);
}
}
我可以得到一些关于如何解决这个问题的帮助吗?它说我的xMin、xMax、zMin和zMax有问题。这应该是一个子弹地狱,让玩家不出界,但如果我不能设置边界,它就无法工作。
我遵循本教程:
(这些都是用C#编写的脚本)
如果有什么帮助的话,我还能得到另一个好的教程吗?来自:
“类型”不包含“成员”的定义,也不包含扩展名
无法找到接受类型为“type”的第一个参数的方法“name”
(是否缺少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
,如下所示:
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)
);
}
}
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)
);