- Home /
Variable not accesible
In my program, a variable isn't available from a void.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
[Space]
[Header("Tiles")]
public GameObject Tiles;
public GameObject[,] tiles = new GameObject[8,8];
public void Start()
{
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);
}
}
tiles[3, 0].transform.position = new Vector3(0f, 10f, 0f); //<-- Here the tiles[3,0] GameObject gets moved to (0f,10f,0f)
}
//On Mouse Click
public void Input()
{
tiles[3, 0].transform.position = new Vector3(1f, 10f, 0f); //<-- But here it doesn't. It says the there's no GameObject in tiles[3,0]
}
}
How are you calling the Input() method? $$anonymous$$ake sure Start() is called before the Input() method is called.
Input is called from the Tile script like this:
public Game$$anonymous$$anager G$$anonymous$$;
public void On$$anonymous$$ouseDown()
{
G$$anonymous$$.Input(position[0],position[1], white, character);
}
And the Input actually looks like this :
public void Input(int i, int j, bool white, int character)
{
tiles[3, 0].transform.position = new Vector3(1f, 10f, 0f);
}
The problem is that inside the Input() $$anonymous$$ethod it can't access the change in the variables. Proof: [Header("Scripts")] public SetSpawner SS; public Figure$$anonymous$$oves F$$anonymous$$;
[Space]
[Header("Tiles")]
public GameObject Tiles;
public GameObject[,] tiles = new GameObject[8,8];
[Space]
[Header("Variables")]
public bool whiteturn = true;
public bool yes = false;
public void Start()
{
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();
F$$anonymous$$.Input(tiles);
TileFiguresN();
yes = true;
}
public void Input(int i, int j, bool white, int character) // Called from Tile script On$$anonymous$$ouseDown()
{
Debug.Log(yes);
}
The output in the console is : false
Answer by belwar · Jun 22, 2018 at 10:33 AM
The problem is that the Input function isn't called when the mouse is clicked.
Use Update method instead:
public void Update()
{
if (Input.GetMouseButtonDown(0))
tiles[3, 0].transform.position = new Vector3(1f, 10f, 0f);
}
If I put a Debug.Log("works") In the Input method it says "works" in the Console so the code is running but it can't find the tiles[] variable
Input function isn't called by default if an input event occurs.
Do you call it from another function? Please share it's code.
it is called from the Tile scripts like this:
public Game$$anonymous$$anager G$$anonymous$$;
public void On$$anonymous$$ouseDown()
{
G$$anonymous$$.Input();
}
I'm leaving out some parts like in Input it sends its position, a bool, a character but that doesn't affect the code.
Answer by Reun_ · Jun 23, 2018 at 09:49 AM
I've created a new problem but with everything in it please check that out: @belwar https://answers.unity.com/questions/1521174/variable-values-not-avalaible-or-they-lost-their-v.html