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. Dismiss Notice

[SOLVED]How to display correct object in scene view

Discussion in '2D' started by crouzilles, Nov 9, 2016.

  1. crouzilles

    crouzilles

    Joined:
    Nov 21, 2015
    Posts:
    25
    Hi all,

    I have a parent game object containing 2 children game objects. The parent is just a container with a script attached. The 2 children game objects are essentially 2 sprites, one showing a disabled box and the other an enabled box (different looks, color, etc.).

    There is a script attached to the parent object which allows the initial setup of the game object via a public enum when it first appears in the scene, either enabled or disabled. Player actions may also enable or disable the box via the same script as the game runs.



    Everything works well in game mode, but in scene view I would like the correct sprite to show depending on the fact the box should be enabled or disabled when added to the scene view while I build the game as I want some boxes enabled by default some not. Is there a way to control what is seen in scene view at all, if so, how?
    Currently both children obejcts have a 0 z position.

    I would like to add that the parent object was made a prefab for ease of use in the building of the game.


    Thank you
    Crouz
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You can use OnValidate to change what is shown at edit time.
    Code (CSharp):
    1. private void OnValidate(){
    2.     switch(boxState){
    3.         case State.Enabled:
    4.             enabledBox.SetActive(true);
    5.             disabledBox.SetActive(false);
    6.             break;
    7.         case State.Disabled:
    8.             enabledBox.SetActive(false);
    9.             disabledBox.SetActive(true);
    10.             break;
    11.     }
    12. }

    Or you can use [ExecuteInEditMode] on the class to have the script run during edit time.
     
    Last edited: Nov 9, 2016
    crouzilles likes this.
  3. crouzilles

    crouzilles

    Joined:
    Nov 21, 2015
    Posts:
    25
    @jeffreyschoch Thank you very much for the clear and concise explanation, very much appreciated.

    Regards
    Crouz
     
    LiterallyJeff likes this.