【发布时间】:2015-03-02 16:04:16
【问题描述】:
我正在尝试开发一个统一游戏,用户可以根据输入到 texbox 的数据移动对象。但我无法将文本框中的数据应用于我想要移动的对象。 这是文本框的 C# 脚本:
using UnityEngine;
using System.Collections;
public class ButtonText : MonoBehaviour
{
private bool defineModel = false;
string beta = "";
public float number ;
bool res ;
void OnGUI()
{
if (!defineModel) {
if (GUI.Button (new Rect (0, 0, 150, 20), "Define a shift"))
defineModel = true;
} else {
beta = GUI.TextField (new Rect (250, 157, 250, 25), beta, 40);
res = float.TryParse(beta, out number);
if (res == false) { print("String is not a number"); }
else { number = float.Parse(beta); }
if (GUI.Button (new Rect (300, 250, 100, 30), "OK")) {
if(!res) return;
else { Debug.Log (" number = " + number);
defineModel = false;
return; }
}
}
}
这里是创建一个在游戏中移动的球体的脚本
using UnityEngine;
using System.Collections;
public class CloneSphere : MonoBehaviour
{
public GameObject sphere;
void Start()
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); // Make a new sphere
sphere.name = "Sphere"; // give it a name
sphere.transform.position = new Vector3(0, 0, 0); // position the sphere
}
float number;
void Update () {
sphere = GameObject.Find("Sphere");
float location = GetComponent<ButtonText>().number ;
sphere.transform.Translate(0, location ,0, Space.World);
}
}
后一个脚本的最后两行产生错误(即,如果我将这两行注释掉,则没有错误): NullReferenceException:对象引用未设置为对象的实例。 CloneSphere.Update () (在 Assets/CloneSphere.cs:20)
这里有什么问题?我怎样才能使这个数字“位置”适用于球体的垂直移动?
【问题讨论】:
-
SO 中的 Unity 标签不适用于 Unity3d 游戏引擎。请使用 unity3d 标签。