使用未分配的局部变量' IsZoneEmpty'

本文关键字:IsZoneEmpty 局部变量 分配 | 更新日期: 2023-09-27 18:08:15

为什么说使用未分配的变量isZoneEmpty。我很难找出原因。因为我在if语句中使用了变量。请帮帮我。为什么说使用未赋值变量isZoneEmpty。我很难找出原因。因为我在if语句中使用了变量。请帮帮我。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnZone : MonoBehaviour 
{
    private Vector3 Zone1;
    private Vector3 Zone2;
    private Vector3 Zone3;
    private Vector3 Zone4;
    private Vector3 Zone5;
    public Vector3 Zone1L;
    public Vector3 Zone2L;
    public Vector3 Zone3L;
    public Vector3 Zone4L;
    public Vector3 Zone5L;
    public GameObject Monster;
    GameObject SpawnedM;
    void Start () 
    {
        Dictionary <Vector3,bool> IsZoneEmpty; new Dictionary <Vector3,bool>();
        {
            IsZoneEmpty.Add(Zone1,true);
            IsZoneEmpty.Add(Zone2,true);
            IsZoneEmpty.Add(Zone3,true);
            IsZoneEmpty.Add(Zone4,true);
            IsZoneEmpty.Add (Zone5,true);
            if (IsZoneEmpty[Zone1] == true) 
            {
                SpawnedM = Instantiate(Monster,Zone1L, Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 1");
            }
        }
    }
}

使用未分配的局部变量' IsZoneEmpty'

Start()方法的第一行应该改为

Dictionary <Vector3,bool> IsZoneEmpty = new Dictionary <Vector3,bool>(); //(`;` to `=`)

你可以通过使用var

让它变小
var IsZoneEmpty = new Dictionary <Vector3,bool>(); 

您应该在C#中使用变量之前初始化它们。