- Home /
Variable values not available or they lost their value
In my program in the Input method, it can access the variables name but it can't access its value. Here the program: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameManager : MonoBehaviour {
#region variables // Every variable is set in editor
[Header("Scripts")]
public SetSpawner SS;
public FigureMoves FM;
[Space]
[Header("Tiles")]
public GameObject Tiles;
public GameObject[,] tiles = new GameObject[8,8]; // this is the only variable not set in the editor
[Space]
[Header("Variables")]
public bool whiteturn = true;
public bool yes = false;
#endregion
public void Start() // Unity calls this Method
{
for (int i = 0; i < 8; i++)
{
Transform parent = Tiles.transform.GetChild(i);
for (int j = 0; j < 8; j++)
{
tiles[i, j] = parent.GetChild(j).gameObject;
tiles[i, j].gameObject.GetComponent<Tile>().PosInput(i, j);
}
}
SS.SpawnNormal(); // This doesn't use any variables from here.
FM.Input(tiles); // this just sends the tiles array so later on FM can use this
yes = true;
TileFiguresN(); // method is later shown
}
public void Input(int i, int j, bool white, int character) // Called from Tile script OnMouseDown() Tile script shown later.
{ // THE PROBLEM IS HERE!
string a;
a = yes.ToString();
bool b = true;
if (Tiles == null)
{
b = false;
}
a = a + b.ToString();
b = true;
if (tiles[0,0] == null)
{
b = false;
}
a = a + b.ToString();
b = true;
if (FM == null)
{
b = false;
}
a = a + b.ToString();
Debug.Log(a); // The output is FalseFalseFalseFalse
}
void TileFiguresN() // This doesn't matter I just put it so you can see the variables doesn't get changed.
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
bool _white;
int character = 0;
if (i == 1 || i == 0)
{
_white = true;
}
else
{
_white = false;
}
if (i == 1 || i == 6)
{
character = 1;
}
if (i == 0 || i == 7)
{
if (j == 0 || j == 7)
{
character = 2;
}
if (j == 1 || j == 6)
{
character = 3;
}
if (j == 2 || j == 5)
{
character = 4;
}
if (j == 3)
{
character = 5;
}
if (j == 4)
{
character = 6;
}
}
tiles[i, j].GetComponent<Tile>().Input(_white, character);
}
}
} //Set's characters to tiles
}
In the console the output is FalseFalseFalseFalse So the variables lost their value. Everything in the Start method and TileFiguresN method is done and correct I've checked it multiple times.
The Tile script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour {
public GameManager GM;
public int[] position = new int[2];
public bool white;
public int character;
public Material avalaible;
public Material original;
public void Start()
{
original = gameObject.GetComponent<Renderer>().material;
}
public void OnMouseDown() //This is the method that calls the INPUT
{
GM.Input(position[0],position[1], white, character);
}
public void PosInput(int i, int j)
{
position[0] = i;
position[1] = j;
}
public void Input(bool _white, int _character)
{
character = _character;
white = _white;
}
public void Color(bool a)
{
if (a)
{
gameObject.GetComponent<Renderer>().material = avalaible;
}
else
{
gameObject.GetComponent<Renderer>().material = original;
}
}
public int ActiveCh()
{
return character;
}
}
Answer by JDelekto · Jun 23, 2018 at 10:52 AM
I think this is because you are using a field initializer to set your tiles[] array which you are not editing You can assign it in your Start() method to initialize it.
Since you are not using the editor to set the tiles[] array, try this:
public void Start() // Unity calls this Method
{
this.tiles = new GameObject[8, 8];
// ... rest of startup code here
}
I had different result, but I did have to fill in the blanks. First, I created two scripts you don't have listed with dummy methods, for SetSpawner and Figure$$anonymous$$oves objects. If these were meant to be simple stand-alone objects, I was not able to set them in the editor. I'm not sure if the intent was to create an use them internally or if it was to attach them as scripts to a game object and then save them in the manager. Anyhow, in my Awake() function on the Game$$anonymous$$anager, I initialized SS and F$$anonymous$$ to a new instance of these objects:
public class SetSpawner
{
public void SpawnNormal() { }
}
public class Figure$$anonymous$$oves
{
public void Input(GameObject[,] tiles) { }
}
// Inside Game$$anonymous$$anager:
public void Awake()
{
this.SS = new SetSpawner();
this.F$$anonymous$$ = new Figure$$anonymous$$oves();
}
Now, from my assumptions, it looks like you have a game object called "Tiles" that you assign to the Tiles property of the Game$$anonymous$$anager, the "tiles" 2-dimensional array appears to be constructed of a hierarchy of game objects. "Tiles" is the parent game object with eight "Tile" children and each tile child has eight "Tile" children beneath them.
I modified the Start() method to assign the Game$$anonymous$$anager to each of the tiles as it processed them in the nested loops:
for (int i = 0; i < 8; i++)
{
Transform parent = Tiles.transform.GetChild(i);
parent.gameObject.GetComponent<Tile>().G$$anonymous$$ = this;
for (int j = 0; j < 8; j++)
{
tiles[i, j] = parent.GetChild(j).gameObject;
tiles[i, j].gameObject.GetComponent<Tile>().G$$anonymous$$ = this;
tiles[i, j].gameObject.GetComponent<Tile>().PosInput(i, j);
}
}
When running the game and clicking on one of my tiles (which I basically added a sprite renderer, a simple sprite and a 2D box collider to get the mouse down events), the output in the debug log was as follows: "TrueTrueTrueTrue"
Was this what you were expecting?
The figuremoves and setspawner is both just a script which could be in the gamemanager script but they are long and use a lots of variable and I don't want them in the gamemanager. And the problem is not that I don't make them a value. If I put the code in the input method in to the start method the output is TrueTrueTrueTrue. So the problem is that after the Start method and before the Input method they just lose their value. Another question with your code why would I make the Tile Object be equal to the Gamemanager object?
Your answer
Follow this Question
Related Questions
Problem when trying to save variable in another variable. 1 Answer
[JS] Adding a scripts variable to a variable 1 Answer
is it better to use MeshFilter[] or three separate MeshFilter var's 1 Answer
comparing variables of gameobjects in an array 2 Answers
Access to components and their variables in Array. 0 Answers