Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make an object non deletable in the hierarchy

Discussion in 'Scripting' started by Reddevildragg, May 18, 2017.

  1. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    50
    Is there a way to make a game object in the hierarchy so it can not be deleted outside of code?

    I am in need of have a group of objects that are created by a script only be able to be removed from that script and not by someone selecting it and clicking delete.

    I found that HideFlags.NotEditable but this will not work as i need to still be able to position and move the object around the scene with the handles and enabling this disables this function. Is there an equivalent system to do this so an object can not be manually deleated but is still fully editable in the scene, basically to do something like hideflags.DontDelete.

    Thanks
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I don't believe so. Is there a reason you need to protect something like that?
     
  3. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    50
    Im trying to build a framework tool that will allow quick setup of projects (having to create several a month that all work around the same) and the editor script relies on various objects being present and called a certain thing. Although this is fine when i use it as i know not to delete them, other people can edit the scene and i ideally want to stop them accidental deleting/renaming the objects and just putting them in the correct positions etc.

    I did not think there was a way but thought i would check :)
     
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    you could try something similar to what the SceneView Camera does. Hide it in the scene and then use editor scripting or gizmos to select and move the object around. Or set the hide flags to show whenever an Editor window is opened and then hide it again when the editor closes. (via OnEnable and OnDisable calls).

    Just be careful to not lose the reference to the hidden object.
     
  5. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    50
    Yeah i was thinking of doing it like this so that they can not "accidentally" delete/rename objects and would have to require a window to be open fist. Like you said its the worry that you lose the reference and then its just always hidden in the scene.

    Im looking into using custom handlers currently and going to investigate into that option.