I have a gameobject with script, the script has a lock boollean and when true it activates gameObject.hideFlags = HideFlags.NotEditable;
But how can I undo this when the user want to be able to select and edit the object/script again?
Or is there another way to make a object unselectable in the scene view
(preferable scene view only and still availeble in the inspector)
There is no c-style cast in UnityScript. UnityScript casts most things automatically. Just use:
gameObject.hideFlags = 0;
The HideFlags enum doesn’t have a constant for 0 so this is the only way.
public enum HideFlags
{
HideInHierarchy = 1,
HideInInspector = 2,
DontSave = 4,
NotEditable = 8,
HideAndDontSave = 13
}
Setting the hideFlags to 8 has the same effect as HideFlags.NotEditable.
edit
Since the hideflags could include other flags as well, it’s better to just affect the flags you want
This will toggle the NotEditable flag, so it can be used to set and reset it:
gameObject.hideFlags ^= HideFlags.NotEditable;
// which is the same as
gameObject.hideFlags = gameObject.hideFlags ^ HideFlags.NotEditable;
This is the xor-operator which is perfect to toggle a single bit in a variable.
To set a certain bit you would do this:
gameObject.hideFlags |= HideFlags.NotEditable;
To reset a certain bit you would do:
gameObject.hideFlags &= ~HideFlags.NotEditable;
// | - bitwise or-operator
// & - bitwise and-operator
// ~ - bitwise negate
Also, in Unity3D version 4.5.5f, the SceneHierarchyView wont refresh, so wont Hide the object right away. If you want to Hide it, you just have to force a refresh like this:
GameObject go = new GameObject( “GameObject” );
DestroyImmediate( go );