public class Node : MonoBehaviour
{
[HideInInspector]
public GameObject turretGo;//保存当前炮塔
}
public class BuildManger : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject() == false)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool iscollider = Physics.Raycast(ray, out hit, 1000, LayerMask.GetMask("Node"));
if (iscollider)
{
Node node = hit.collider.GetComponent<Node>();
if (node.turretGO == null) //这里在Unity里报错
{ //可以创建
if(Money > selectedTurretDesign.cost)
{
MoneyChange(-selectedTurretDesign.cost);
node.BuildTurret(selectedTurretDesign.Prefab);
}
}
}
报错原因NullReferenceException: Object reference not set to an instance of an object