Unity [Unity] Arrays

This topic is 867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
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 on other sites

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

Edited by Juliean

Share on other sites

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 on other sites

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 on other sites

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 on other sites

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 on other sites

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 on other sites

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 on other sites

Thanks Wyrframe, thats exactly where I was making my mistake.

• 10
• 14
• 11
• 10
• 11
• Similar Content

• Hello fellow devs!
Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

In Pseudo-Code:
angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
index = (int) (angle / (PI / 5));
PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

I can't get my head around it. Any suggestions? Is the whole approach doomed?

Thank you in advance for any input!

• Hi , I was considering this start up http://adshir.com/, for investment and i would like a little bit of feedback on what the developers community think about the technology.
So far what they have is a demo that runs in real time on a Tablet at over 60FPS, it runs locally on the  integrated GPU of the i7 . They have a 20 000 triangles  dinosaur that looks impressive,  better than anything i saw on a mobile device, with reflections and shadows looking very close to what they would look in the real world. They achieved this thanks to a  new algorithm of a rendering technique called Path tracing/Ray tracing, that  is very demanding and so far it is done mostly for static images.
From what i checked around there is no real option for real time ray tracing (60 FPS on consumer devices). There was imagination technologies that were supposed to release a chip that supports real time ray tracing, but i did not found they had a product in the market or even if the technology is finished as their last demo  i found was with a PC.  The other one is OTOY with their brigade engine that is still not released and if i understand well is more a cloud solution than in hardware solution .
Would there  be a sizable  interest in the developers community in having such a product as a plug-in for existing game engines?  How important  is Ray tracing to the  future of high end real time graphics?

• Good day,

I just wanted to share our casual game that is available for android.

Description: Fight your way from the ravenous plant monster for survival through flips. The rules are simple, drag and release your phone screen. Improve your skills and show it to your friends with the games quirky ranks. Select an array of characters using the orb you acquire throughout the game.