Access array from another script and return new value for cell

Heho!
I’m new here, but I’ve been abusing your knowledge for quite some time now. Sadly I am not a programmer, but studying arts. To be more independent I decided to learn a little bit of coding and so far I am having a lot of fun with it :slight_smile:

At the moment I am trying to make a simple grid based game, and I have problems with returning the variables back to the grid, which is stored as an 2D-array in another script.

I have a walking script that looks like this:

At the beginning of the function, which is called once a second:

    public void StartWalking() {

        //return position to array
        int xaxis = (int)this.transform.position.x;
        int zaxis = (int)this.transform.position.z;
        Grid.ground[xaxis, zaxis] += 1;

        Debug.Log(Grid.ground[xaxis, zaxis] + this.name);

This raises the value of the currently selected cell with 1. Later I’ll make a jarred array to actually store the information which object is on the field, but i figured I’d first try the easy version.

And this is at the end of the function (After the object is moved) to empty the old cell that is now either empty or has other objects on it:

        // set cell back to empty
        if ((xaxis!= this.transform.position.x) || (zaxis!=this.transform.position.z))
        {
            Grid.ground[xaxis, zaxis] -= 1;
        }
    }

And in the Grid script I only have this one line:

using UnityEngine;
using System.Collections;
public class Grid : MonoBehaviour {
      static public int[,] ground = new int[10, 10];
}

My problems:
If I have multiple objects, they all keep their own grid-arrays. How do I make them all use and update the same array, the one in the other script?
I thought if I make the array static that should work, but it doesn’t.

Also if I only have one object, but don’t set the cells back on the end of the method, does work and basically counts how often the object visited a certain cell… So in general my idea seems to be right, the only problem is that they don’t all use the same array.

(from here on I don’t have any clue about programming and am just guessing)
I thought this probably could work if I get the xaxis and zaxis values of each object, that’s why I made the method public, but I don’ really know how to call it from the walking script into the grid script. Also later on I want to make new objects spawn, so the grid would need to call a lot of random other objects - Does that even work?

I hope this is not too confusing, I neither speak programmer nor englisch :). But I’d be so happy if someone could help me a little bit! As you can see on my thread title, I don’t even know how to properly search for a solution.

Cheers,
Lena

I’ve got a similar setup. I have a grid of tiles that I want all classes to access. For example, each character in the game will need to access the tiles. Each tile itself may need to get info about other tiles etc.

I created a separate class called TileManager. This stores all of the info about each tile and has various methods for modifying a given tile eg:

public bool IsPlayerTile(int x, int y)
{
      return tiles[x,y].GetComponent<TileInfo>().IsPlayerTile;
}

There is a base class that has a static reference to the tilemanager and all other classes inherit from this.

public class BaseClass
{
    protected static TileManager tileManager;
}

public class CharacterClass : Base Class
{
   // code here
}

However, in direct answer to your question. Look at the static keyword on variable definition. A static variable is only defined once. All instances of that class will share the same reference. To implement this:

public class BaseClass
{
   protected static int[,] ground =new int[10, 10];
}

public class CharacterClass : Base Class
{
   // code here
}

public class EnemyClass : Base Class
{
   // code here
}

The character and enemy class will share the same reference to the ‘ground’ variable. There will only be one instance of the ‘ground’ variable.

To make this available to all classes make the ‘ground’ variable ‘public’ then you can access it like so: BaseClass.Ground[7, 8];

1 Like

Wow, thanks for the fast answer! I guess I have to learn a lot, especially about not writing everything after each other in one script.

I already made the array for the grid static:

      static public int[,] ground = new int[10, 10];

And them I am accessing it like you wrote:

  Grid.ground[xaxis, zaxis] += 1;

But still, when I have two objects on the same field, they both return “1” instead of “2” (like they should) as a value.

Did I do anything else wrong? Should I maybe post the whole code? (It’s not that much, only those two scripts :))

Hmm, if they’re both accessing the same static variable it should work fine.

If the codes short, then yeah,post what you got :slight_smile:
BTW, convention is to have the access modifier first (public before static: public static int[,] etc) :slight_smile:

1 Like

Thank you again!

I actually found the solution. I was first setting the value to 1 and then resetting it in the same method to 0. This way the value could actually never be higher than 1. :smile:

Thank you so much for your help! :slight_smile: And I’ll change the public static thing :smile:

1 Like