Search Unity

[SOLVED]SetActive not working

Discussion in 'Scripting' started by Bisoncraft, May 5, 2020.

  1. Bisoncraft

    Bisoncraft

    Joined:
    Feb 5, 2016
    Posts:
    37
    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.

    Code (CSharp):
    1.  
    2. public class UnitPanelController : PanelController
    3.     {      
    4.         public GameObject bodyStatus;            //this is the reference to the gameObject I want to deactivate
    5.  
    6.         private void AssignUnit(ID newUnitID)
    7.         {
    8.             if (newUnitID != null && newUnitID != ID.NoOne)
    9.             {
    10.                 unitID = newUnitID;
    11.                 unit = GameData.allDataObjects[unitID] as Unit;
    12.                 bool unitHasBody = (unit.Body != null);
    13.                 Debug.Log(this + "-bodyStatus not null, unit has body = " + unitHasBody);
    14.                 if (bodyStatus != null)
    15.                 {
    16.                     Debug.Log("1-bodyStatus.activeInHierarchy = " + bodyStatus.activeInHierarchy);
    17.                     bodyStatus.SetActive(unitHasBody);
    18.                     Debug.Log("2-bodyStatus.activeInHierarchy = " + bodyStatus.activeInHierarchy);
    19.                 }                Title.UpdateTitle(unit.Name);
    20.             }
    21.             else
    22.             {
    23.                 Debug.LogError("Assigned null or NoOne Unit to UnitPanel");
    24.             }
    25.         }
    26.  
    27.     }
    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.
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    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.
     
  3. Bisoncraft

    Bisoncraft

    Joined:
    Feb 5, 2016
    Posts:
    37
    I'm pretty sure I'm pointing to the right object.
    In fact, I can even change its name - as below:
    upload_2020-5-6_8-7-10.png

    Code (CSharp):
    1.  
    2.         private void AssignUnit(ID newUnitID)
    3.         {
    4.             if (newUnitID != null && newUnitID != ID.NoOne)
    5.             {
    6.                 unitID = newUnitID;
    7.                 unit = GameData.allDataObjects[unitID] as Unit;
    8.                 bool unitHasBody = (unit.Body != null);
    9.                 Debug.Log(this + "-bodyStatus not null, unit has body = " + unitHasBody);
    10.                 if (bodyStatus != null)
    11.                 {
    12.                     Debug.Log("1-bodyStatus.activeInHierarchy = " + bodyStatus.activeInHierarchy);
    13.                     bodyStatus.SetActive(unitHasBody);
    14.                     if (!unitHasBody)
    15.                     {
    16.                         bodyStatus.name = "THIS SHOULD BE DEACTIVATED";
    17.                     }
    18.                  
    19.                     Debug.Log("2-bodyStatus.activeInHierarchy = " + bodyStatus.activeInHierarchy);
    20.                 }                Title.UpdateTitle(unit.Name);
    21.             }
    22.             else
    23.             {
    24.                 Debug.LogError("Assigned null or NoOne Unit to UnitPanel");
    25.             }
    26.         }
    27.  
    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.
     
  4. Bisoncraft

    Bisoncraft

    Joined:
    Feb 5, 2016
    Posts:
    37
    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.

    Code (CSharp):
    1.         private static void OpenPanel(ID newUnitID)
    2.         {
    3.             GameObject newUnitPanelObject = Instantiate(InterfaceManager.Instance.unitPanelPrefab);
    4.             UnitPanelController newUnitPanel = newUnitPanelObject.GetComponent<UnitPanelController>();
    5.             newUnitPanelObject.transform.SetParent(InterfaceManager.Instance.transform);
    6.             newUnitPanelObject.transform.localPosition = newUnitPanel.positionAtStart;
    7.             newUnitPanel.Open();
    8.             Debug.Log("newunitpanel active:" + newUnitPanelObject.activeSelf);
    9.             unitPanels.Add(newUnitPanel);
    10.             newUnitPanel.SetActiveDebugObjects();
    11.             Debugger.instance.SwitchDebugEvent += newUnitPanel.SetActiveDebugObjects;
    12.             newUnitPanel.AssignUnit(newUnitID);
    13.         }
    14.  
    I instantiate the new panel on line 3, and then I assign the unit on line 12.
     
  5. Bisoncraft

    Bisoncraft

    Joined:
    Feb 5, 2016
    Posts:
    37
    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.
     
    arfish likes this.
  6. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    178
    i fixed this using boolean that is set where needed and recalls the method in a LateUpdate() and sets the boolean back to false
     
    Alaadel likes this.
  7. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    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);
    }

    }

    }
     
    Last edited: Dec 22, 2022
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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://forum.unity.com/threads/using-code-tags-properly.143875/

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