Sign in to follow this  
Adehm

Unity [Unity] Arrays

Recommended Posts

Adehm    218

I'm new to Unity and trying to store dynamic public variable arrays to be used throughout my project. I declare my array inside of my script but when I try to initialize I am told it is out of scope. How do I access it in the same script and how do I access it from another script?

using UnityEngine;
using System.Collections;
public class GameLoop : MonoBehaviour 
{
 public int[,] HOME = new int[4, 1]; //[Resource,Capacity]
    void Start ()
    {
       HOME[0, 0] = 0; //Resource0 stored
       HOME[0, 1] = 100; //Resource0 storage capacity
    }
}

Share this post


Link to post
Share on other sites
Nypyren    12074
Accessing it in the same script should work like you have written.

However, this line will give you an IndexOutOfRangeException:

HOME[0, 1] = 100;

...because [4,1] means "4 x 1", which means you have [0 to 3, 0] as the valid range you can plug in.

public int[,] HOME = new int[4, 1];



Accessing your HOME variable in another script requires your other script to get access to the GameLoop instance somehow. In Unity you have several options. If the other script is attached to the same GameObject, you can say:

public class OtherClass : MonoBehaviour
{
   void Start()
   {
      var gameloop = GetComponent<GameLoop>();
      if (gameloop != null)
      {
          gameloop.HOME[0,0] = 0; // or whatever.
      }
   }
}
If your component is on a different GameObject, there are a variety of other ways to handle it. You could store a static reference to your GameLoop:

public class GameLoop : MonoBehaviour
{
    public static GameLoop Instance;
    
    void Awake()
    {
        Instance = this;
    }
}
and then your other script would change to this:

public class OtherClass : MonoBehaviour
{
   void Start()
   {
      var gameloop = GameLoop.Instance;
      if (gameloop != null)
      {
          gameloop.HOME[0,0] = 0; // or whatever.
      }
   }
}
or you could do this:

public class OtherClass : MonoBehaviour
{
   void Start()
   {
      var gameloop = GameObject.FindObjectOfType<GameLoop>();
      if (gameloop != null)
      {
          gameloop.HOME[0,0] = 0; // or whatever.
      }
   }
}


I would also suggest not using all-capital variable names. "HOME" should be "Home" instead, since it's public. If it was private, it should be "home".

Share this post


Link to post
Share on other sites
Juliean    7077

public int[,] HOME = new int[4, 1]; //[Resource,Capacity]

 

You are declaring a 2D array with 4*1 entries,

which looks like this:

0 0 0 0

Now what your access code is doing is accessing it like that:

0 // 0/0
0 // 0/1

I'm not entirely sure what you are trying to do here, but you eigther need to need to increase the vertical size of the array, or change something about your approach.

 

EDIT: Ninja'd, damn smile.png

Edited by Juliean

Share this post


Link to post
Share on other sites
Adehm    218

Okay so I think I’m completely misunderstanding how multidimensional arrays work in C#. I’m coming from Basic where I would use an array to catalog information. Home[0,] would be like my container and every dimension after would relate to the first dimension. The declaration Home[1,1] would mean (0,0), (0,1), (1,0), and (1,1) would all be separate pieces of information.

 

So in C# Home[1,1] would be the same as Home[1] because it means Home[1x1], giving me Home[0] and Home[1] to hold information?

 

Home[2,2] would give me Home[0-4], Home[2,2,2] would give me Home[0-8]?

Share this post


Link to post
Share on other sites
Juliean    7077

So in C# Home[1,1] would be the same as Home[1] because it means Home[1x1], giving me Home[0] and Home[1] to hold information?



Home[2,2] would give me Home[0-4], Home[2,2,2] would give me Home[0-8]?
 

 

Not quite. Home[X,Y] are coordinates into the array. Both start at 0. So 1,1 gives you 1 into the array horizontally, and 1 vertically. The formula for the linear array key is actually:

Y * width + X

Since multidimensional arrays are actually ordered line by line:

   1 2 3 4 -> // 0/0 - 3/0
-> 5 6 7 8    // 0/1 - 3/1

So you can access this whole array in the range of Home[0,0] to Home[3,1].

Edited by Juliean

Share this post


Link to post
Share on other sites
Adehm    218

Not quite. Home[X,Y] are coordinates into the array. Both start at 0. So 1,1 gives you 1 into the array horizontally, and 1 vertically. The formula for the linear array key is actually:

Y * width + X

Since multidimensional arrays are actually ordered line by line:

   1 2 3 4 -> // 0/0 - 3/0
-> 5 6 7 8    // 0/1 - 3/1

So you can access this whole array in the range of Home[0,0] to Home[3,1].

 

 

Had me at coordinates but then lost me again, sorry.

yy

xx

Home[1,1] should give me 4 right?

1

0 1

But how do I write it?

My understanding of this was…

Home[0,0] would be the 0

Home[0,1] would be the 1 above 0

Home[1,0] would be the 1 after the 0.

Home[1,1] would be above the 1.

 

Again I'm sorry it hasent clicked for me yet.

Share this post


Link to post
Share on other sites
Juliean    7077

Had me at coordinates but then lost me again, sorry.

yy

xx

Home[1,1] should give me 4 right?

1

0 1

But how do I write it?

My understanding of this was…

Home[0,0] would be the 0

Home[0,1] would be the 1 above 0

Home[1,0] would be the 1 after the 0.

Home[1,1] would be above the 1.

 

Again, not quite. 1,1 gives you 6 - it accesses 2 elements in horizontally, and then 2 vertically.

 

Maybe this should make it clear:

    0 1 2 3 X

0   1 2 3 4 
1   5 6 7 8
2   9 1 2 3
3   4 5 6 7

Y

Any combination of X/Y from this table for an square 2d array will give you the element in that row/colum, 0/2 will give you 9 e.g. This works for every form of 2D-array, just put first access index as "X", second as "Y", and fill this table due to width/height.

 

Aside from that, I strongly second phil_t's suggestion.

Edited by Juliean

Share this post


Link to post
Share on other sites
lede    591

I have to agree with phil_t about changing this to a different type of data structure that will provide you a little more flexibility in your game. 

 

Array's suffer from not being able to expand them very easily because their one large contiguous segment of memory.  This provides for very fast access to your data elements but at the cost of searching for something will be cost you lots of cpu cycles.  Granted only have an array 4x4 isn't a large amount of data so the extra time wouldn't cost you much.  If you have to expand the array though as it gets larger the time will exponentially increase for finding elements in the array.

Share this post


Link to post
Share on other sites
Wyrframe    2426

Adehm; you're misunderstanding how indices work, in contrast to how array allocation works.

 

public int[,] HOME = new int[4, 1];

 

This gives you an array of 1 by 4 cells. It's your choice to call those columns and rows or rows and columns.

 

But arrays are what is called "0-indexed." A one-dimensional array of size 5 (say, int[] list = new int[5]) has valid indexes 0 and 1 and 2 and 3 and 4; five total cells, starting at cell number zero. Similarly, a two-dimensional array of size 4,1 has valid indexes 0,0 and 1,0 and 2,0 and 3,0... and that's it. To store two "fields" per "row", you want new int[4,2], and index them at R,0 and R,1.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this