【发布时间】:2014-07-21 12:19:50
【问题描述】:
using UnityEngine;
使用 System.Collections;
公共类 GameHandler : MonoBehaviour {
public GameObject playerPrefab;
public GameObject playerCameraPrefab;
public GameObject player;
public GameObject playerCamera;
// Use this for initialization
void Awake () {
player = Instantiate(playerPrefab,new Vector3(5,1,5),Quaternion.identity) as GameObject;
playerCamera = Instantiate(playerCameraPrefab,Vector3.up,Quaternion.identity) as GameObject;
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
使用 System.Collections; 使用 System.Collections.Generic;
公共类 EarthMagic : MonoBehaviour {
public GameHandler gameHandler;
public GameObject EarthMagicWall;
public PlayerMovement PM;
public UIControls UI;
public Transform spinner;
public List<Transform> circleStones = new List<Transform>();
// Use this for initialization
void Start () {
//connect to the gameHandler
gameHandler = GameObject.Find ("WorldHandler").GetComponent("GameHandler") as GameHandler;
PM = transform.GetComponent("PlayerMovement") as PlayerMovement;
UI = transform.GetComponent("UIControls") as UIControls;
}
// Update is called once per frame
void Update () {
if(UI.gamePaused == false) {
if(Input.GetButtonDown("Channel")) {
if(PM.isGrounded == true) {
Vector3 checkPosition = transform.position + (transform.forward * 3);
// Debug.DrawRay(transform.position, checkPosition, Color.blue, 20);
// Debug.DrawRay(checkPosition, transform.TransformDirection (Vector3.down), Color.red, 20);
RaycastHit hit;
if (Physics.Raycast(checkPosition, transform.TransformDirection (Vector3.down), out hit, 5)) {
if(hit.collider.name == "Terrain") {
Instantiate(EarthMagicWall, hit.point, transform.rotation);
}
}
}
}
if(Input.GetMouseButtonDown(0)) {
if(circleStones.Count > 0) {
Transform temp = circleStones[0].transform;
circleStones.RemoveAt(0);
temp.parent = null;
temp.rigidbody.AddForce(Camera.main.transform.up * 20);
}
}
ControlRocks ();
}
}
void ControlRocks() {
foreach(Transform circleStone in circleStones) {
switch(circleStones.Count) {
case 1: case 2:
if(circleStones.IndexOf(circleStone) == 1) {
circleStone.transform.localPosition = new Vector3(0,0, 2);
} else {
circleStone.transform.localPosition = new Vector3(0,0, -2);
}
break;
case 3:
if(circleStones.IndexOf(circleStone) == 1) {
circleStone.transform.localPosition = new Vector3(0,0, 2);
} else if(circleStones.IndexOf(circleStone) == 2) {
circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, (Mathf.Sqrt(2) * -1));
} else {
circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, (Mathf.Sqrt(2) * -1));
}
break;
case 4:
if(circleStones.IndexOf(circleStone) == 1) {
circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, Mathf.Sqrt(2));
} else if(circleStones.IndexOf(circleStone) == 2) {
circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, (Mathf.Sqrt(2) * -1));
} else if(circleStones.IndexOf(circleStone) == 3){
circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, (Mathf.Sqrt(2) * -1));
} else {
circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, Mathf.Sqrt(2));
}
break;
case 5:
if(circleStones.IndexOf(circleStone) == 1) {
circleStone.transform.localPosition = new Vector3(0,0, 2);
} else if(circleStones.IndexOf(circleStone) == 2) {
circleStone.transform.localPosition = new Vector3(1.9f,0, 0.62f);
} else if(circleStones.IndexOf(circleStone) == 3){
circleStone.transform.localPosition = new Vector3(1.18f,0, -1.61f);
} else if(circleStones.IndexOf(circleStone) == 4){
circleStone.transform.localPosition = new Vector3(-1.18f,0, -1.61f);
} else {
circleStone.transform.localPosition = new Vector3(-1.9f,0, 0.62f);
}
break;
}
Stone stone = circleStone.gameObject.GetComponent("Stone") as Stone;
if(!stone.isRigid) {
Rigidbody shardRigidBody = stone.gameObject.AddComponent<Rigidbody>();
shardRigidBody.mass = 5;
stone.isRigid = true;
}
}
}
}
所以上面的错误发生在 EarthMagic 的第 63 行。我以前解决过这种类型的错误,但这次我真的不明白。这段代码以前可以工作,我没有更改整个函数。然而,我确实开始实例化我的播放器和相机,而不是简单地将它们放在编辑器中。我确定这是问题的原因,但我真的不知道如何解决这个问题。有什么想法吗?
【问题讨论】:
-
第 63 行是哪一个? ;)
-
您有公共变量,需要将 GameObjects 拖到它们的位置,以便它们知道它们所引用的内容...确保您已修复这些框并返回您的结果...如果您已经已经完成了,那么我们需要您提供更多信息
-
是的,我已经完成了 Savlon。你需要了解什么?第 63 行是 circleStone.transform.localPosition = new Vector3(0,0, -2);
标签: unity3d runtime-error