Unity3d Unity C#NullReferenceException:对象引用未设置为对象的实例
使用系统集合 公共类GameHandler:MonoBehavior{Unity3d Unity C#NullReferenceException:对象引用未设置为对象的实例,unity3d,runtime-error,Unity3d,Runtime Error,使用系统集合 公共类GameHandler:MonoBehavior{ using UnityEngine; using UnityEngine; } 使用系统集合; 使用System.Collections.Generic 公共类地球魔法:单一行为{ using UnityEngine; using UnityEngine; 公共游戏处理程序游戏处理程序; 公共游戏对象EarthMagicWall; 公众游乐场运动项目经理; 公共用户界面; 公共变换微调器; public List c
using UnityEngine;
using UnityEngine;
}
使用系统集合;
使用System.Collections.Generic
公共类地球魔法:单一行为{
using UnityEngine;
using UnityEngine;
公共游戏处理程序游戏处理程序;
公共游戏对象EarthMagicWall;
公众游乐场运动项目经理;
公共用户界面;
公共变换微调器;
public List circleStones=新列表();
//用于初始化
无效开始(){
//连接到gameHandler
gameHandler=GameObject.Find(“WorldHandler”).GetComponent(“gameHandler”)作为gameHandler;
PM=transform.GetComponent(“PlayerMovement”)作为PlayerMovement;
UI=转换.GetComponent(“UIControls”)为UIControls;
}
//每帧调用一次更新
无效更新(){
如果(UI.gamePaused==false){
if(Input.GetButtonDown(“通道”)){
如果(PM.isground==真){
Vector3检查位置=transform.position+(transform.forward*3);
//Debug.DrawRay(transform.position,checkPosition,Color.blue,20);
//Debug.DrawRay(checkPosition,transform.TransformDirection(Vector3.down),Color.red,20);
雷卡斯特击中;
if(物理.光线投射(检查位置,变换.变换方向(矢量3.向下),外击,5)){
如果(hit.collider.name==“地形”){
实例化(EarthMagicWall、hit.point、transform.rotation);
}
}
}
}
if(Input.GetMouseButtonDown(0)){
如果(circleStones.Count>0){
Transform temp=圆环石[0]。Transform;
圆环石。移除(0);
temp.parent=null;
临时刚体添加力(Camera.main.transform.up*20);
}
}
控制岩石();
}
}
孔隙控制岩石(){
foreach(在圆环石中变换圆环石){
开关(圆圈。计数){
案例1:案例2:
if(圈石.圈石指数)==1){
circleStone.transform.localPosition=新向量3(0,0,2);
}否则{
circleStone.transform.localPosition=新向量3(0,0,-2);
}
打破
案例3:
if(圈石.圈石指数)==1){
circleStone.transform.localPosition=新向量3(0,0,2);
}else if(circleStone.IndexOf(circleStone)==2){
circleStone.transform.localPosition=新向量3(Mathf.Sqrt(2),0,(Mathf.Sqrt(2)*-1));
}否则{
circleStone.transform.localPosition=新向量3((Mathf.Sqrt(2)*-1),0,(Mathf.Sqrt(2)*-1));
}
打破
案例4:
if(圈石.圈石指数)==1){
circleStone.transform.localPosition=新向量3(Mathf.Sqrt(2),0,Mathf.Sqrt(2));
}else if(circleStone.IndexOf(circleStone)==2){
circleStone.transform.localPosition=新向量3(Mathf.Sqrt(2),0,(Mathf.Sqrt(2)*-1));
}else if(circleStone.IndexOf(circleStone)==3){
circleStone.transform.localPosition=新向量3((Mathf.Sqrt(2)*-1),0,(Mathf.Sqrt(2)*-1));
}否则{
circleStone.transform.localPosition=新向量3((Mathf.Sqrt(2)*-1),0,Mathf.Sqrt(2));
}
打破
案例5:
if(圈石.圈石指数)==1){
circleStone.transform.localPosition=新向量3(0,0,2);
}else if(circleStone.IndexOf(circleStone)==2){
circleStone.transform.localPosition=新矢量3(1.9f,0,0.62f);
}else if(circleStone.IndexOf(circleStone)==3){
circleStone.transform.localPosition=新矢量3(1.18f,0,-1.61f);
}else if(圈石索引of(圈石)==4){
circleStone.transform.localPosition=新矢量3(-1.18f,0,-1.61f);
}否则{
circleStone.transform.localPosition=新矢量3(-1.9f,0,0.62f);
}
打破
}
石头石头=圆形石头.gameObject.GetComponent(“石头”)作为石头;
如果(!stone.isRigid){
刚体shardRigidBody=stone.gameObject.AddComponent();
shardRigidBody.mass=5;
stone.isRigid=true;
}
}
}
}
所以上面的错误发生在EarthMagic的第63行。我以前解决过这类错误,但这次我不太明白。这段代码以前是有效的,我没有改变整个函数。不过,我确实开始实例化我的播放器和相机,而不是简单地将它们放在编辑器中。我肯定这是问题的原因,但我真的不知道如何解决这个问题。有什么想法吗?列表中有一个空元素。这是很多代码,你应该自己调试,它会帮助你 现在,您可以实现这个简单的Hack
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;
}
}
}
此代码将检查null,如果找到,则只跳过该元素 哪一个是第63行?;)你有公共变量,需要将游戏对象拖到它们的位置,以便它们知道它们引用的是什么…确保你已经修复了这些框,并将结果发回…如果你已经这样做了,那么我们需要你提供更多的信息是的,我已经这样做了Savlon。你需要知道什么?第63行是circleStone.transform.localPosition=新向量3(0,0,-2);可能是怪异的复制品。这解决了大部分问题,其余的问题我自己解决了,但为什么我以前不需要呢?我试着调试它,但被卡住了。所以我来这里寻求帮助。无论如何,谢谢。这真的让我很困扰。你在代码中做了一些使一个或多个元素为空的事情。祝你比赛好运!