I have a new issue in my code.
I have created a Unit panel, showing information on a Unit. This panel is a UI panel, with a script attached to it, called UnitPanelController, and I have saved it as a prefab. During runtime, if I click on a Unit in my game, the interface will open the panel by instantiating it from the Prefab.
Depending on the type of unit, I want to deactivate a child object of this panel at opening. So I’ve created a variable for this panel controller storing the reference to this child object. Until that point, everything works fine.
But then, at this point, SetActive doesn’t work.
I’ve been searching for similar issues, but I’m pretty sure they don’t apply to my case:
- my code is running from an active object. The unit panel controller script works, and the Debug Log works as well;
- the parent object is active, so I have no issue there.
Here are some parts of my code. The code that doesn’t work is on line 17.
public class UnitPanelController : PanelController
{
public GameObject bodyStatus; //this is the reference to the gameObject I want to deactivate
private void AssignUnit(ID newUnitID)
{
if (newUnitID != null && newUnitID != ID.NoOne)
{
unitID = newUnitID;
unit = GameData.allDataObjects[unitID] as Unit;
bool unitHasBody = (unit.Body != null);
Debug.Log(this + "-bodyStatus not null, unit has body = " + unitHasBody);
if (bodyStatus != null)
{
Debug.Log("1-bodyStatus.activeInHierarchy = " + bodyStatus.activeInHierarchy);
bodyStatus.SetActive(unitHasBody);
Debug.Log("2-bodyStatus.activeInHierarchy = " + bodyStatus.activeInHierarchy);
} Title.UpdateTitle(unit.Name);
}
else
{
Debug.LogError("Assigned null or NoOne Unit to UnitPanel");
}
}
}
The result of my Debug.Log lines is:
UnitPanel(Clone) (Fortuna.UnitPanelController)-bodyStatus not null, unit has body = False
1-bodyStatus.activeInHierarchy = True
2-bodyStatus.activeInHierarchy = False
So basically the code seems to be working; the object bodyStatus is deactivated as it should be… but is still displayed within the game.
I’ve also checked that the bodyStatus gameObject is the correct one, during the game, and Unity sends me to the right object.
I have no idea what’s wrong here. Does any one have any idea?
P.S. I can change the name of the ‘bodyStatus’ object, it works - so this is the right object I’m trying to deactivate. But SetActive still doesn’t work.