Greetings!
I have a very specific problem that I could not find answer elsewere, so I come here for help! first post ever, so, apologies for any mistakes.
I’m learning unity, and I thought it would be interesting, in a turn based game, to get the canvas of the character enabled only when he is taking his turn.
I have another script that instantiate clones of prefabs that represent my characters, each have their own canvas, that for now, display a single ‘end turn’ button, and I have another script that is supposed to manage their turns.
At runtime the prefabs spawn. In the Unity Editor, the script that manages the turns properly has every component associated in the script in the inspector panel, and when i press the button to display one clone’s canvas, it just does nothing, and does not display any errors.
I declare these arrays at the beginning of the script:
public GameObject[] characters;
[SerializeField] private CharacterAtributes[] characterAtributes;
[SerializeField] private Canvas[] characterCanvas;
Later, I call this function to get references for instantiated characters inside the arrays:
void CharacterFinder()
{
for (int i = 0; i < 4; i++)
{
characters[i] = GameObject.Find("PlayerCharacter" + (i + 1));
characterAtributes[i] = characters[i].GetComponent<characterAtributes>();
characterCanvas[i] = characters[i].GetComponentInChildren<Canvas>(includeInactive: true);
}
}
Then I made this button just to test the management of the canvases:
public void CharacterTurnReady()
{
characterAtributes[0].speed -= 20.0f;
characterCanvas[0].enabled = true;
Debug.Log("character canvas: " + characterCanvas[0]);
if characterCanvas[0].enabled)
{
Debug.Log("Canvas enabled: " + characterCanvas[0].name);
}
else
{
Debug.Log("Canvas disabled: " + characterCanvas[0].name);
}
}
And, after pressing the main canvas button, the debuglog says it is enabled, but the button inside the character canvas doesnt appear, and then later i ran another debug in the ‘Update’ function, and it says the canvas is enabled even without pressing this button that is supposed to enable it.
And then i look in the inspector panel, the canvas that i want to change is properly referenced in the array.
Does not do what i want and shows no errors in the log. Dont know what to do (T_T).