1

i´ve been working on a game in Unity and created a gamefield consisting of Transform objects. Now i wanted to integrate a simulation of the game,which is not running on the actuall gamefield, but on a clone of the gamefield which is not visible for the player ( this is needed for a AI algorithm im working on). Unitl now this is what i´ve got to clone the gamefield:

public Transform[,] vertricalArray;
public Transform[,] horizontalArray;
public Transform[,] boxes;
public Transform[,] sim_Box;
public Transform[,] sim_Vertical;
public Transform[,] sim_Horizontal;

for (int y = 0; y <= 3; y++) {

           for (int x = 0; x <= 3; x++)

           {


              if (x != 3 && y != 3)
                   sim_Box[x, y] = Instantiate(boxes[x,y], transform.position, transform.rotation) as Transform;

              if (y != 3)
                   sim_Vertical[x, y] = Instantiate(vertricalArray[x,y], transform.position, transform.rotation) as Transform;

             if (x != 3)
                  sim_Horizontal[x, y] = Instantiate(horizontalArray[x,y], transform.position, transform.rotation) as Transform;

           }

       }

this is supposed to clone the objects from the gamefield (verticalArray etc.) into the simulation gamefield (sim_Vertical etc.) Whenever i try to start this though, i get the error, that the sim_ arrays are null and i can´t copy those objects into the sim_ arrays. Would appreciate help and also if someone knows a better way to create a gamefield for the simulation im very open for ideas!

Thanks!

Joe Kehr
  • 27
  • 3
  • 1
    Are you sure you initialize array? It sould look like: Transform[,] sim_array = new Transform[sizex, sizey](); You can use array only after initalization (memory allocation). If you not do it, you have null-pointer in you variable. – Sergiy Klimkov Oct 16 '17 at 15:35
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sergiy Klimkov Oct 16 '17 at 15:36
  • Please only use relevant tags, neither `ai` or `montecarlo` are relevant tags, and I think it'd be a push to say that `simulation` is. – George Oct 16 '17 at 16:31
  • Hey @SergiyKlimkov thanks for your answer! i checked and noticed that i initialized the arrays wrong. But now after i copied the arrays i can not alter the original arrays. Whenever i try to change something in the initial arrays (horizontalArray,vertricalArray,boxes) it only changes in the copied arrays...isn´t Instantiate supposed to create a clone without reference? – Joe Kehr Oct 16 '17 at 17:53
  • I don't how are you cloning the array. But check this: https://msdn.microsoft.com/ru-ru/library/system.array.clone(v=vs.110).aspx – Sergiy Klimkov Oct 16 '17 at 18:52

0 Answers0