Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Trouble scripting SceneVisibilityManager

Discussion in 'Scripting' started by petey, Apr 30, 2020.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Are you using UnityEditor namespace? Seems like that's where it is according to the docs.
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Ahh I was doing something silly :rolleyes: Got it working now, but I am getting this error -

    ScriptableSingleton already exists. Did you query the singleton in a constructor?
    UnityEditor.SceneVisibilityManager:.ctor()


    Here's my code for creating it. I thought the if null would stopped that error -
    Code (CSharp):
    1.         if (sv == null)
    2.         {
    3.             sv = ScriptableObject.CreateInstance<SceneVisibilityManager>();
    4.         }
    Any ideas?
    Thanks
    Pete
     
  4. julienh_unity

    julienh_unity

    Unity Technologies

    Joined:
    Oct 25, 2017
    Posts:
    12
    I might have just tried to do exactly what you were trying to do.
    I was not familiar with the concept of ScriptableSingleton either, and found your post.
    In the end, this seems to work, very simply:

    Code (CSharp):
    1. var sv= SceneVisibilityManager.instance;
    2. sv.DisablePicking(go,false);
    No need to check for null, the instance will be created if it does not exists.

    Important note, because I had that issue:
    If you did call :
    ScriptableObject.CreateInstance<SceneVisibilityManager>();

    For some reason you will get the
    ScriptableSingleton already exists. Did you query the singleton in a constructor? 
    error message every time
    SceneVisibilityManager.instance
    is called. But the code is good. Restarting Unity will remove the error messages. It looks like for some reason the additional instances created by ScriptableObject.CreateInstance persist even after domain reload.
     
    Last edited: May 4, 2020
    Rioneer, petey and ThermalFusion like this.
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I've not really used SceneVisibilityManager, but from my limited tests it seems you need to use the instance provided by SceneVisibilityManager.instance. This seems to exist in 2020.x versions, but not 2019, not sure how it works in the latter.
    I've not really seen this kind of pattern used before, almost all other Editor API are static.

    edit: Read post above ^^
     
    petey and julienh_unity like this.
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Got it, thanks for the help :)