I’m making a turn based combat system. When the player’s turn is done the UI’s SetActive turns and it calls the Enemy’s function to begin their turn. When the Enemy’s turn is done it calls a function from the player’s code to set the UI back to true. However the UI is not activating again. The enemyAI code and the playerbattlecontrol code are NOT parented to the UI so I don’t understand why neither of them can turn the UI gameObject back on!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class BattleCharControl : MonoBehaviour
{
GameObject batUI;
GameObject targetList;
GameObject[] _numberOfEnemies;
GameObject buttonPressed;
BaseCharacter character;
private void Start()
{
batUI = GameObject.Find("BattleUI");
batUI.SetActive(true);
TurnKeeper.playerTurn = true;
targetList = batUI.transform.Find("TargetList").gameObject;
_numberOfEnemies = GameObject.FindGameObjectsWithTag("Enemy");
character = gameObject.GetComponent<BaseCharacter>();
}
public void targetSelect()
{
buttonPressed = EventSystem.current.currentSelectedGameObject;
buttonPressed.SetActive(false);
targetList.SetActive(true);
int i = 0;
while(i < _numberOfEnemies.Length)
{
i++;
targetList.transform.Find("target" + i.ToString()).gameObject.SetActive(true);
int a = i - 1;
targetList.transform.Find("target" + i.ToString()).gameObject.transform.Find("Text").gameObject.GetComponent<Text>().text = _numberOfEnemies[a].GetComponent<BaseCharacter>().characterName;
}
}
public void confirmTarget()
{
string action = "";
switch (buttonPressed.name)
{
case "AttackButton":
action = "attack";
break;
}
string targetNo = EventSystem.current.currentSelectedGameObject.name.Substring(6);
int indexTarg = int.Parse(targetNo) - 1;
BaseCharacter target = _numberOfEnemies[indexTarg].gameObject.GetComponent<BaseCharacter>();
targetAction(target,action);
}
public void targetAction(BaseCharacter target, string action)
{
switch (action)
{
case "attack":
target.attacked("attack");
endTurn();
break;
}
}
public void endTurn()
{
Debug.Log("Player's turn is over");
int ran = Random.Range(0, _numberOfEnemies.Length);
_numberOfEnemies[ran].gameObject.GetComponent<EnemyAI>().choosePlayer();
batUI.SetActive(false);
}
public void hi()
{
batUI.SetActive(true);
Debug.Log("Enemy turn is over");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
GameObject[] players;
GameObject batManager;
BaseCharacter character;
BaseCharacter target;
private void Start()
{
players = GameObject.FindGameObjectsWithTag("PlayerChar");
batManager = GameObject.FindGameObjectWithTag("Manager");
character = gameObject.GetComponent<BaseCharacter>();
}
public void choosePlayer()
{
if(character.health <= 0)
{
enabled = false;
}
int i = players.Length;
int ran = Random.Range(0, i);
target = players[ran].gameObject.GetComponent<BaseCharacter>();
enemyAction(target);
}
public void enemyAction(BaseCharacter target)
{
string name = character.characterName;
switch (name)
{
case "BoxMan":
int a = BoxMan.attacks.Length;
int ran2 = Random.Range(0, a);
string action = BoxMan.attacks[ran2];
target.attacked(action);
batManager.GetComponent<BattleCharControl>().hi();
break;
default:
Debug.Log("This enemy's name does not match any enemy class! Check for syntax errors.");
break;
}
}
}
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
The function is running for sure. I have the EnemyAI script call the hi() function in CharBattleControl. I know it’s running because when that function runs it prints “Enemy turn is over” into my output window. So the only thing that seems to not be working is the set active in my function, because the Debug.Log works perfectly fine. Here lemme give you just the function I’m talking about here:
public void hi()
{
batUI.SetActive(true);
Debug.Log("Enemy turn is over");
}