我有可变范围问题吗?

时间:2017-08-17 12:14:02

标签: c# nullreferenceexception

我在Unity写作并不是所有类都可以实例化,因为它们继承了Monobehaviour。我有几个类,我试图将一个数组返回到一个类,但继续得到一个空引用异常。 我确保数组已编入索引并初始化,但它返回null。 但是在没有数组的相同构造中,例如Int,它可以工作。

public class StartUp : MonoBehaviour  //Unity class
{
        public int x = 30,y = 30;
        public PlayField entireField;

        void Awake()
        {
            entireField = new PlayField(x,y);
            entireField.InitArray(entireField, x, y);
        }
}

public class CharacterController : MonoBehaviour
{
       //edit: playField is not supposed hold value, just to get
       //access to a method in PlayField
       PlayField playField; 

       FieldData fData;           

       void Start(){
           playField = new PlayField();
           fData = new FieldData();
       }
       void Update(){
          fData = playField.GetFieldData(5,6); //We're just telling it go to that method 
       } 
}
public class PlayField
{
       public SingleField[,] singleField;

       public PlayField()
       {

       }
       public PlayField(int x, int y)
       {
              singleField = new SingleField[x,y];
       }

       void InitArray(PlayField playfield, int x, int y){
              //In an effort to save the data I also did
              this.singleField = new SingleField[x,y];

              for(int j ...
              {
                  for (int x...
                  {
                      playfield.singleField[x, y] = new SingleField();
                      playfield.singleField[x, y].fielData = new FieldData();
                      playfield.singleField[x, y].fielData.height = GetRandom();
                      //and here
                      this.singleField[x,y] = new SingleField();
                      this.singleField[x,y].fieldData = new FieldData();
                  }
              }
              //to ..
              this.singleField = playfield.singleField;
       }

       public FieldData GetFieldData(int x, int y){

            //Here it keeps returning null reference
            return this.singleField[x,y].fieldData;
       }
}

public class SingleField
{
       public FieldData fieldData;
       public GameObject fieldObject;
}
public class FieldData
{
        public float height;
        public Vector3 position = new Vector3();
}

我知道我可以使用静态变量,但我想知道我在这里做错了什么,或者我如何使用none-MonoBehaviour类PlayField将StartUp类中的wholeField值传递给CharacterController中的FieldData fData? 我以为this.singleField-array会有值,但是在来自CharacterController的Method调用期间没有?

2 个答案:

答案 0 :(得分:1)

Erkan,我认为你需要退后一步,重新思考你是如何接近这一点的。这就是我说的原因:

   //edit: playField is not supposed hold value, just to get
   //access to a method in PlayField
   PlayField playField; 

......这不是事情的运作方式。如果你有一个'主'PlayField来保存你的所有值,并初始化另一个...任何时候你在第二个上调用函数,它将使用第二个的值。在您的情况下,您正在尝试调用GetFieldData函数,该函数将从PlayField的第二个实例中获取字段数据 - 而不是您要使用的那个。< / p>

那你从哪里开始?

首先,您可能想要了解面向对象编程概念。我并不是想成为居高临下的人 - 但如果你对这些程序没有很好的掌握,那么继续你的计划就很难。

其次,考虑使用'静态'。 Static说,“我的整个程序中只有一个例子”。以下面的例子为例:

class Playfield
{ /* code */ }

class SomeAlreadyExistingMainClassInYourCode
{
  static Playfield globalFieldInstance = new Playfield();
  /* rest of the class */
}

...此时,你有一个可在整个程序中访问的Playfield:SomeAlreadyExistingMainClassInYourCode.globalFieldInstance。

你不应该过度使用/滥用它 - 但是如果你只打算创建一个对象的单个实例,那么创建它的静态实例是一个很好的方法。

答案 1 :(得分:0)

@ m.rogalski是的,我试图避免使用静态,以及它是如何开始的。

解决了空引用错误。 真的没想到我必须初始化所有内容,因为我只想在另一个类中调用方法*并使用同一类的另一个实例返回。

public class CharacterController : MonoBehaviour
{
   //edit: playField is not supposed hold value, just to get
   //access to a method in PlayField
   PlayField playField; 

   FieldData fData;           

   void Start(){
       playField = new PlayField();
       InitArray(playField,30,30);
       fData = new FieldData();
   }
  void InitArray(PlayField playfield int x, int y){
       playfield.singleField = new SingleField[x,y];
       for (int j =0; j< y;.. {
            for (int i..  {
                playfield.singleField[x,y] = new SingleField();
                playfield.singleField[x,y].fieldData = new FieldData();
            }
       }
  }
   void Update(){
      //The return was not supposed to be a field in this instance of
      //PlayField, but come from the PlayField class itself as 
      //this.singleField[x,y] (a array in PlayField)
      fData = playField.GetFieldData(5,6); //We're just telling it go to that method 
   } 
}

public class PlayField
{
    public SingleField[,] singleField = new SingleField[30,30];//evrything initailized and so on in the rest of the class here ommited. ect.

    public FieldData GetFieldData(int x,int y){
         //Here was the catch. Return values form this Class
         // and not read from the instance created in CharacterController
         //was giving me a nullrefence while I was using only FieldData
         return this.singleField[x,y].fieldData;
    }
}

这对我来说已经解决了。 接下来的问题。 感谢