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