• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Alexia
      Hi all,
      A small team are currently undertaking a fairly large game project involving multi-playing, taming and other complex game mechanisms.
      We are looking for someone who is experienced in programming a wide range of mechanisms, more information is on our discord server. It is also a learning experience and we wouldn't expect you to know how to do everything we wanted, but just ask that you would be willing to learn how to.
      If you are interested in joining the (rather long term project) just message me and i'll add you on discord.
      Thanks for your time,
      Alexia 
    • By davemacdo
      I'm a formally trained composer (doctorate from Michigan State) who writes what most people would call avant-garde concert music. I love weird abstract projects, and I would like to work with somebody making a weird, abstract, artsy game. 
      You can find more about me and my music on my site. I have worked with acoustic and electronic sounds, including some procedurally generated and interactive computer music. 
      In particular, I would like to work on a project that lets me use Fmod to prepare an adaptive score for a game built on Unity or Unreal. I've been a music professor and would like to get experience working in this medium so that I can be a better mentor for my students. Send me a DM or email <davidjohnmacdonald@gmail.com> if you would like to discuss working on a project together. 
    • By Tuner_z

      Name: One Level: Stickman Jailbreak
      Price: Free
      Developer: RTU Studio
      Platform: Android
      Language: C# (Unity3D)
      Google Play: https://play.google.com/store/apps/details?id=com.RTU.OneLevel
       
      Hello!
      I want to show you my game! "One level: Stickman Jailbreak" is a puzzle game with unusual gameplay where you must help the character to escape from prison. You just need to take the key and get out alive. The game has only one level, and there are many ways to complete it. Not everything is as simple as it might seem at first glance, so there are clues in the game.
       
      Short description:
      Nobody escapes from here!
       
      Description:
      Tommy got into trouble again! Our hero is behind bars. But he's not going to stay in jail for a long time and he decides to escape. Tommy steals a key and gets out of the jail cell. But our friend doesn't go free: Tommy suddenly finds himself in the same room from which he just escaped! The conditions for escaping change every time. In order to go free Tommy will have to solve logical puzzles and you can help him in this!
      At first it will be easy, but the tension will increase, and the tasks will become more complicated with each level. You should use your brain for all 100%, but if your skill is not enough, you can use a hint or ask for help from friends!
      You can solve the puzzles alone or with your friends and spend time well!
       
      Features: 
      Features:
      - 48 unique levels;
      - the game is translated into 10 languages: English, French, German, Spanish, Italian, Portuguese, Russian, Japanese, Chinese, Korean;
      - the function of "help from friend";
      - hints;
      - instructions.
       
      Trailer:
       
      Screenshots:





       
    • By Brandon Marx
      Hello forum,
      I have some decent amount of experience in Unity making games for Software Engineering projects in college, these were very specific projects however and I still am fairly new to building games. I wanted to make a game that uses the shadows of objects for collision detecting (i.e. shooting a gun at a characters shadow causes that character damage. What is the best engine to do this in (game will be 3D), and does anyone have any advice on how to approach this concept? I consider myself fairly experienced in programming, but game dev is just an entirely different beast.
    • By juicyz
      Hey all,
      I've been slowly working on my game called AotW for a while now.  I have come to the conclusions that it would be nice to cooperate with 1 or 2 others to help finish it.  Ive been trying to keep my GDD up to date with my ideas and development so that would give a better overview of the game when the time comes.  Currently I have a basic skeleton of the RPG elements needed but everything can still be discussed and talked about and we can transform my idea to something the group likes.
      The premise of the game is a Diablo-like procedurally generated map with RPG elements that include sockets, inventory, classes, abilities, crafting, loot, items, sockets, and enchanting.  This will be done in a 2D iso view as I can't do 3D art and I enjoy 2D games a lot.
       
      I don't plan on releasing this as this is more of a hobby project for me and I have a full-time job.  Though I'd like to start putting more hours into development and having others definitely will be motivation.  I also want to be able to say I have finally "finished" a game idea to some degree.  If the time comes and we want to release it, then we can go ahead and do so but that's not my purpose or plan. 
       
      Discord:
      Juicyz#3683
       
       
      Thanks,
      Juicyz
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity [Unity] Arrays

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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

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

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

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

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

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

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

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
Sign in to follow this  

  • Advertisement