- Home /
Need help cycling through an array with a 'for' loop.
Ok, I am attempting to create a tile-based turn-based game. I am in the early stages of it and am trying to just get my fog-of-war / tile assignments to work. The way it should play is that you have scouts and you send them to a tile that is "fogged out" it sets a bool variable curScouting to true. Then when the turn is over (henchmen assigned to tasks) you hit the end turn button. I am trying to set up an array of strings to hold the tile-names (based on coordinates from center, center eg. -2 / -2). I then want the EndTurn script to step through each map to access the Fog_Tile script (on each tile) where I can check for the curScouting flag and set some more flags that result in the tile being revealed. I am running into errors cycling through things. The error message I get is "NullReferenceException: Object reference not set to an instance of an object EndTurn.OnMouseUp () (at Assets/Reclamation/EndTurn.cs:33)"
Here is my script: "using UnityEngine; using System.Collections; using System.Collections.Generic;
public class EndTurn : MonoBehaviour {
public string[] Map = new string[] {"-2 / 2", "-1 / 2", "0 / 2", "1 / 2", "2 / 2", "-2 / 1", "-1 / 1", "0 / 1", "1 / 1", "2 / 1",
"-2 / 0", "-1 / 0", "Castle", "1 / 0", "2 / 0", "-2 / -1", "-1 / -1", "-1 / 0", "-1 / 1", "-1 / 2", "-2 / -2", "-1 / -2",
"0 / -2", "1 / -2", "2 / -2"};
private int mapTiles = 25;
public Henchmen scouts; // points to Henchmen script to manipulate scout henchy #
public Fog_Tile scouting; // points to Fog tile script to manipulate fog flags
void Start ()
{
scouts = GameObject.Find ("Castle").GetComponent();
}
void OnMouseUp()
{
for (int i = 0; i < (mapTiles - 1); i++)
{
scouting = GameObject.Find (Map[i]).GetComponent();
if (scouting.curScouting == true)
{
scouting.curScouting = false;
scouting.scouted = true;
scouts.scouts += 1;
}
}
}
void Update () {
}
} // end of line"
SO - I've set up a for loop and am trying to use the variable 'i' that I use to advance to loop to also advance the array (Map[i]) so it will step through each tile name and can then access the Fog_Tile script on it. so... what am I doing wrong here?
I'm thinking PERHAPS I want to revers my logic and use my EndTurn script to set a flag and have each individual Fog_Tile script check for that flag; and I think I could pull that off, but want to understand where I am going wrong with for loops and arrays in case I want to use that in the future.
as you can guess this is incredibly messy! :)
(1) generally don't use arrays, use List. there is zero performance issue at hand here and it is incredibly, staggeringly, clearer and easier to use
(2) you need a pool I'd say, or some form of List handling of your objects
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
(3) you need to get more with OO program$$anonymous$$g. almost certainly here you should have .. some sort of .. object that does something. those, err, things would do things on their own, and all this code would totally disappear
(4) if you explain what you are trying to do VERY SI$$anonymous$$PLY in terms of the game, someone will tell you how!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Push down GUI everytime for loop cycles through Array 1 Answer
Values in Array Automatically Revert 1 Answer
Distribute terrain in zones 3 Answers
Affect every object in array. 1 Answer