Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Invalid behavior of Component Extension SetActive #903

Closed
FejZa opened this issue Nov 20, 2021 · 5 comments · Fixed by #905
Closed

Invalid behavior of Component Extension SetActive #903

FejZa opened this issue Nov 20, 2021 · 5 comments · Fixed by #905
Labels
Bug Something isn't working

Comments

@FejZa
Copy link
Contributor

FejZa commented Nov 20, 2021

XRTK - Mixed Reality Toolkit Bug Report

Describe the bug

XRTK's component extension SetActive

public static void SetActive(this Component component, bool isActive)
        {
            if (component.gameObject.activeInHierarchy != isActive)
            {
                component.gameObject.SetActive(isActive);
            }
        }

is error prone and it differs from people would expect when setting a GameObject active / inactive. activeInHierarchy is not the same as activeSelf. If an object is not activeInHierarchy but activeSelf calling SetActive(false) using this extension will result in the object not being set to inactive.

@FejZa FejZa added the Bug Something isn't working label Nov 20, 2021
@StephenHodgson
Copy link
Contributor

It's a lot easier to set a component active/inactive using the enabled property.

This extension method was meant to be used purely to enable/disable the game object that the component is attached to.

@FejZa
Copy link
Contributor Author

FejZa commented Nov 20, 2021

Yes, but enabled is again a whole different story.

"This extension method was meant to be used purely to enable/disable the game object that the component is attached to."

Exactly, and that is what it is failing to do.

@FejZa
Copy link
Contributor Author

FejZa commented Nov 20, 2021

IF the parent game object is disabled but the game object that it is attached to is not, then SetActive(false) won't disable it.

@FejZa
Copy link
Contributor Author

FejZa commented Nov 20, 2021

The fix would be:

if (component.gameObject.activeSelf != isActive)
            {
                component.gameObject.SetActive(isActive);
            }

@StephenHodgson
Copy link
Contributor

Makes sense to me. Thanks for catching this! 🙏

FejZa added a commit that referenced this issue Nov 22, 2021
StephenHodgson pushed a commit that referenced this issue Nov 23, 2021
XRTK-Build-Bot pushed a commit that referenced this issue Jan 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants