unity中给自定义类中的属性赋值,报错“Object reference not set to an instance of an object”

2025-05-10 00:08:54
推荐回答(2个)
回答1:

改成这样
using UnityEngine;
using System.Collections;

public class ChessBoard : MonoBehaviour {
Coordinate[,] chessBoardPos;
void Start () {
chessBoardPos = new Coordinate[8,8];
chessBoardPos [1, 1] = new Coordinate ();
chessBoardPos[1,1].x= 1.111f;
}
}

你这样相当于
chessboardpos[1,1] = null;
chessboardpos[1,1].x = 1.111f
是空的咋赋值都会告诉你
Object reference not set to an instance of an object

回答2:

是不是没有实例化