我目前迷路了,因为我的GameObject返回null。
关于这个问题,我不确定是不是因为我错过了一件小事,或者我直接编程完全错误地使用了多个类。
我生成gameobject数组的代码:
public class gridInit : MonoBehaviour {
const int mapWidth = 33;
const int mapHeight = -27;
const float tileSizeUnits = 1.0f / 3;
public GameObject tile;
// Use this for initialization
void Start () {
GenerateTileMap();
Screen.fullScreen = true;
}
void Awake()
{
}
// Update is called once per frame
void Update () {
}
bool walkable = false;
[HideInInspector]
public GameObject[] goTile = new GameObject[892];
void GenerateTileMap()
{
int tileNumber = 0;
for (float y = 0; y > mapHeight; y--)
{
for (float x = 0; x < mapWidth; x++)
{
float tilePosX = x/3;
float tilePosY = y/3;
int layerPos = -2;
goTile[tileNumber] = Instantiate(tile, new Vector3(tilePosX, tilePosY, layerPos), Quaternion.identity) as GameObject;
goTile[tileNumber].name = "tile" + tileNumber;
goTile[tileNumber].tag = "grid";
RaycastHit2D hit;
Vector3 fwd = goTile[tileNumber].transform.TransformDirection(Vector3.forward);
Debug.DrawRay(goTile[tileNumber].transform.position, fwd * 50, Color.green);
hit = Physics2D.Raycast(goTile[tileNumber].transform.position, fwd, 200);
if (hit)
{
goTile[tileNumber].tag = "walkableGrid";
}
tileNumber++;
}
}
}
我想要使用的课程设置GameObjects:
public class gameCalibration : MonoBehaviour {
bool calibrationComplete = false;
public gridInit gInit;
public cam Cam;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
{
for (int i = 0; i < gInit.goTile.Length; i++)
{
if (calibrationComplete == false)
{
gInit.goTile[i].GetComponent<Renderer>().enabled = true;
}
else
{
gInit.goTile[i].GetComponent<Renderer>().enabled = false;
}
}
if (calibrationComplete == false)
{
Calibrate();
}
else
{
gInit.GenerateObjects();
calibrationComplete = true;
}
}
}
Vector2[] camTileCenter;
Vector2 tempCenter;
void Calibrate()
{
for (int i = 0; i < gInit.goTile.Length; i++)
{
gInit.goTile[i].GetComponent<SpriteRenderer>().color = Color.red;
if (Cam.redBlobs[i] != null && camTileCenter[i] != tempCenter)
{
camTileCenter[i] = new Vector2(Cam.redBlobs[i].Width / 2, Cam.redBlobs[i].Height / 2);
tempCenter = camTileCenter[i];
}
}
}
}
我已经尝试过搞乱一些值以及如何初始化类,但是没有占上风。
我的完整错误:
NullReferenceException: Object reference not set to an instance of an object
gameCalibration.Update () (at Assets/Scripts/gameCalibration.cs:21)
gameCalibration中的错误行:gInit.goTile[i].GetComponent<Renderer>().enabled = true;
答案 0 :(得分:2)
public gridInit gInit = new gridInit();
而不是
public gridInit gInit;
我没有看到你在任何地方创建对象的实例
答案 1 :(得分:0)
问题是类case RAM_ADDR is
when "000" =>
RAM_ADDR <= "001";
when "001" =>
RAM_ADDR <= "010";
when "010" =>
RAM_ADDR <= "011";
when "011" =>
RAM_ADDR <= "100";
when "100" =>
RAM_ADDR <= "101";
when "101" =>
RAM_ADDR <= "110";
when "110" =>
RAM_ADDR <= "111";
when "111" =>
RAM_ADDR <= "000";
when others =>
RAM_ADDR <= "000";
end case;
尚未实例化,例如采取这种情况
gridInit
当你遇到这种情况时
public List<string> exS;
public List<string> exS2 = new List<string>();
Console.WriteLine(exS.Count); //The List<string> is null, as it has not been instantiated - errors
Console.WriteLine(exS2.Count); //the List<string> is blank, as it was instantiated with new List<string>() - prints 0
现在你觉得这里发生了什么? public gridInit gInit;
public gridInit gInit2 = new gridInit();
gInit.goTile[i].GetComponent<Renderer>().enabled = false;
gInit2.goTile[i].GetComponent<Renderer>().enabled = false;
为null,因为它没有实例化,而gInit
有效,因为当我们使用gInit2
时,它创建了类的新实例,然后创建了它的变量