2015-08-16 35 views
0

为什么说使用未分配的变量isZoneEmpty。我很难找出原因。因为我在if语句中使用了变量。请帮助我。为什么说使用未分配的变量isZoneEmpty。我很难找出原因。因为我在if语句中使用了变量。请帮助我。使用未分配的本地变量`IsZoneEmpty'

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"); 
      } 
     } 
    } 
} 
+1

您声明了一个变量但不会初始化它。 'Dictionary IsZoneEmpty;''你需要像'Dictionary IsZoneEmpty = new Dictionary ();' – Eser

+0

我在代码中做了。 –

+1

@DanSingh不,你没有 – dotnetom

回答

1

第一行中的Start()方法应该只是利用var

var IsZoneEmpty = new Dictionary <Vector3,bool>(); 

你应该initialize variables before using them in C#变为此

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

你可以把它变小。

+2

感谢downvote。请解释原因,以便我可以改进答案并让社区变得更美好。 – dotctor

+0

在统一中,开始不需要拥有;。而你的权利,我甚至没有收到这种感谢。 –

+2

我没有Downvote。 –

相关问题