Search Unity

SceneVisibilityManager error

Discussion in 'Scripting' started by abermann_O, May 26, 2021.

  1. abermann_O

    abermann_O

    Joined:
    Aug 16, 2018
    Posts:
    42
    Hello everyone,

    I need to hide some GOs from the Scene view (meaning, turning off the eye icon in the Hierarchy). So I found this seems the way to go:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.Universal;
    4. using UnityEngine.UI;
    5. using UnityEditor;
    6.  
    7.  
    8. [ExecuteInEditMode]
    9. public class CameraZonesVector : MonoBehaviour
    10. {
    11. SceneVisibilityManager.instance.Hide(this.gameObject, true);
    12. }
    If I drop this script in a GO, it works just fine: it hides both the parent GO and all its childs.

    The problem is that —even if I don´t use the script in the scene— when I try to make an Android build, it complains saying this:

    error CS0103: The name 'SceneVisibilityManager' does not exist in the current context

    Any hints on this? I hardly find any references in the web to this issue...

    Thank you all in advance, have a great day!
     
  2. You really shouldn't mix game and editor code. But if you really-really-really want to, always put your editor code in
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. [...] // unity editor code goes here
    3. #endif
    including your use of
    using UnityEditor;
    .

    BTW, what is that class you put here? It looks 'funny'. Maybe you're missing a method declaration and stuff? :D
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    abermann_O likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    This... ^ ^ ^ and for certain applications you can also use partial classes to stick parts of your code in a completely-#ifdeffed-out file (that has to be in the same assembly obviously, not in a folder called Editor!)
     
    abermann_O likes this.
  5. mishakozlov74

    mishakozlov74

    Joined:
    Aug 8, 2018
    Posts:
    143
    Hey, so, the problem is that there is two main namespaces - UnityEngine and UnityEditor.
    The first one is wrapper over c++ engine, which we call from C# in order to create game logic. It contains all the classes we need for that.

    The second one, UnityEditor dedicated for Editor-specific things, like, sliders, editors, internal UI and so on.
    One of these is SceneVisibilityManager.

    The problem is, you can't ship your game with UnityEditor classes, because you have it only in context of Editor.
    So, by the end of the day, you have to get rid of all the UnityEditor usages in your runtime code.

    But I assume you want to hide your object in runtime, not in Editor.
    There are a few ways to do that, but the most easiest one is to just disable it.

    https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

    If you want it to keep working, but being invisible, probably I'd put your visible part of it a into child and disable this child.
     
    abermann_O likes this.
  6. abermann_O

    abermann_O

    Joined:
    Aug 16, 2018
    Posts:
    42
    Lurking-Ninja, Kurt-Dekker and mishakozlov74, thank you so much for your time to answer my question! With all the knowledge you shared with me so generously, I´ll fix this problem, and upload the script, in case anyone may find it useful for their own work.

    Again, guys, no words to thank you! Have a great day!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Of course you're welcome. Always good to see someone diligently tracking down an unexplained situation and trying all kinds of reasonable things to figure out what was going on, and then you made a good post explaining your whole process, which makes it easy for us to look at and go "Ah, I bet it was this issue..."

    Dive right in, the water's fine! Unity3D is the best game engine ever. Get it all over your brain and you will learn more and more amazing things and eventually you will even impress yourself with what you can do. :)