[SOLVED]SetActive not working

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.

Hi,

My tips is to take a look while running the game in the Editor to see what’s going on with the GameObjects. Perhaps there are doubles of the instantiated objects, where some are still active while others are inactivated.

I’m pretty sure I’m pointing to the right object.
In fact, I can even change its name - as below:
5809732--614770--upload_2020-5-6_8-7-10.png

        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);
                    if (!unitHasBody)
                    {
                        bodyStatus.name = "THIS SHOULD BE DEACTIVATED";
                    }
                  
                    Debug.Log("2-bodyStatus.activeInHierarchy = " + bodyStatus.activeInHierarchy);
                }                Title.UpdateTitle(unit.Name);
            }
            else
            {
                Debug.LogError("Assigned null or NoOne Unit to UnitPanel");
            }
        }

In that case, the unit doesn’t have a body component, so the name of the component has been changed on line 16. The Debug.Log also says this very same object (bodyStatus) is deactivated. However, it appears active in the hierarchy.

I’m wondering whether the reason might be that I try to deactivate this bodyStatus at the same time as I instantiate the game object, so maybe it wouldn’t be open in the hierarchy. However that wouldn’t explain why the debug log tells me it’s active before initialisation.

Here is the code that I use to create the new Unit Panel Controller.

        private static void OpenPanel(ID newUnitID)
        {
            GameObject newUnitPanelObject = Instantiate(InterfaceManager.Instance.unitPanelPrefab);
            UnitPanelController newUnitPanel = newUnitPanelObject.GetComponent<UnitPanelController>();
            newUnitPanelObject.transform.SetParent(InterfaceManager.Instance.transform);
            newUnitPanelObject.transform.localPosition = newUnitPanel.positionAtStart;
            newUnitPanel.Open();
            Debug.Log("newunitpanel active:" + newUnitPanelObject.activeSelf);
            unitPanels.Add(newUnitPanel);
            newUnitPanel.SetActiveDebugObjects();
            Debugger.instance.SwitchDebugEvent += newUnitPanel.SetActiveDebugObjects;
            newUnitPanel.AssignUnit(newUnitID);
        }

I instantiate the new panel on line 3, and then I assign the unit on line 12.

Just for info, I’ve fixed the issue. I’m not entirely sure what was going wrong, but my code was a bit complicated. I inherited some methods from a general PanelController, I had a separate method for opening the panel, and after cleaning some of that mess, things are now working correctly.

1 Like

i fixed this using boolean that is set where needed and recalls the method in a LateUpdate() and sets the boolean back to false

1 Like

Hi someone knows why the gameObject desactivates but not Activates??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GuardarPistola : MonoBehaviour
{ public GameObject Gun;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

if(Input.GetKeyDown(KeyCode.E))
{
Gun.SetActive(!Gun.activeInHierarchy);
}

}

}

Please start a new post, don’t necro-post on old stuff.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

If a GameObject is inactive, Update() would not be called on it.