嘿所以我正试图让我的投资组合进入一个扫雷游戏进入一所学校。但我遇到了一个小问题。我正在使用团结。 我一直收到这个错误: NullReferenceException:未将对象引用设置为对象的实例 SceneInitializer.Start()(在Assets / Scripts / SceneInitializer.cs:23)
我有两个脚本一起工作。 它在某个时候起作用了。但现在即使我回去它也有NULL错误。 违规行标有!!!!
这是脚本一。抛出错误的那个。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SceneInitializer : MonoBehaviour {
public GameObject BoardTilePrefab;
void Start () {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
GameObject tile = AddTile (BoardTilePrefab);
tile = Instantiate (tile);
//Get script and atatch it to game object
tile.AddComponent <Tile> ();
tile.GetComponent <Tile> ().SetBomb ();
tile.GetComponent <Tile> ().SetPosition (x, y);
//Sets the position in world space
tile.transform.position = tile.GetComponent <Tile> ().GetPosition();
//Sets tag and position elements in other script
!!!!!tile.tag = ConvertGetPositionToTag(GetComponent<Tile>().GetPosition());
}
}
}
//Methods
public GameObject AddTile (GameObject GameTile)
{
GameObject Tile = GameTile;
return Tile;
}
private string ConvertGetPositionToTag (Vector2 position) {
int positionX = (int) position.x;
int positionY = (int) position.y;
string sPositionX = positionX.ToString ();
string sPositionY = positionY.ToString ();
string toTag = sPositionX + sPositionY;
return toTag;
}
}
这是脚本二。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour {
private bool bomb;
public bool isPressed;
public bool nextToBomb;
private Vector2 position;
//SetBom
public bool SetBomb ()
{
float randNumb = Random.Range (0.0f, 1.0f);
if (randNumb == 1) {
return this.bomb = true;
} else {
return this.bomb = false;
}
}
//GetBomb
public bool GetBomb ()
{
return bomb;
}
//SetPosition
public void SetPosition (int x, int y) {
this.position = new Vector2 (x, y);
}
public Vector2 GetPosition (){
return position;
}
public void SetPressed () {
this.isPressed = false;
}
//if Tile is bomb
//You lose
//if Tile is not bomb
//SetPressed true
//Check if next to bomb
//if that tile has bomb Set NexttoBomb True
//if that tile has no bomb
//GetPosition tag of other tile
//use "Check if next to bomnb" if tile is not pressed to check its neighbours
}
关于代码的任何类型的提示或指针也都是高级的。 谢谢你的帮助!!!