- Home /
How do I lock an object from selection
In other programs like Photoshop, After Effects, 3dsmax, Maya you can lock objects or layers so you don't select them by error when working in the viewport. I want to lock my background in my 2D game.
I have tried a temporal solution to turn off the Mesh renderer from the object. (but is not exactly a lock from selection since I can't view it anymore).
Answer by Magrippinho · Feb 11, 2014 at 07:48 PM
You've probably noticed by now, but Unity 4.3, released a couple of months after your question, added native Layer locking functionality!
This is a great solution to avoid accidental selection in the Scene view, but note that it doesn't prevent selection and editing from the Hierarchy panel.
It's an imperfect solution because it only lets you lock by layer and not by object.
Thanks! This was exactly what I was looking for. However, it doesn't make much sense without the picture because I didn't even know that "Layers" button was up there until now.
I know this is an old thread, but does anyone know how to toggle layers programmatically? I'm looking for an editor script to do this
What you were looking for is probably this: https://answers.unity.com/questions/760533/best-way-to-set-a-gameobject-layer-programmaticall.html
Answer by SolidAlloy · May 24, 2020 at 01:34 PM
There is a way to make a single object unselectable in Unity 2019.3 and newer. There is a hand icon to the left of an object in the hierarchy window. Clicking it toggles between blocking and allowing to select the object on the scene. More info at the following link, in the "Setting GameObject visibility and pickability section": https://docs.unity3d.com/Manual/Hierarchy.html
This is definetely a better answer with the newest version of the editor.
This is what I was looking for but while this is great, it doesn't persist after restarting the project :/
Answer by CarlLee · Mar 12, 2021 at 03:08 AM
It's been a long time since the question has been asked. Just in case someone is still interested, there is a way to make object unselectable via click in scene. Simply add a pass with tag "LightMode" = "Picking"
in your shader and discard its result instead of returning the correct _SelectionID
. This is how Unity picks objects on screen fast.
Pass
{
Name "Picking"
Tags { "LightMode" = "Picking" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap
#include "UnityCG.cginc"
float4 vert(appdata_full v) : SV_POSITION
{
UNITY_SETUP_INSTANCE_ID(v);
float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
return mul(UNITY_MATRIX_VP, worldPos);
}
uniform float4 _SelectionID;
fixed4 frag() : SV_Target
{
return _SelectionID;
}
ENDCG
}
Add this pass to your shader, your object will be unselectable in SceneView, but still selectable in hierarchy window.
That's actually a neat hacky solution. Until now I haven't really thought about how Unity does the object picking. However, does this also prevent selection through a selection rectangle? It not it's a half baked solution.
Thank you I am very interested but I'm not quite sure I understand this solution, on which shader should we add this pass ?
Answer by itos · Sep 04, 2013 at 09:31 PM
To answer my own question: I found an utility in the Unity Asset Store that does exactly what I want. You can lock objects so you can't edit them and also configure it in the preferences so you can't select them in the viewport. Also is free so anyone can use it. (I'm not affiliated or know the person that made the plugin).
Answer by FrimaMickD · Sep 04, 2013 at 09:07 PM
There is no built-in way to do this in Unity (or at least I'm not aware of it!) but you could always script it quite easily. Here is a link to another question that pretty much solve the same thing : http://answers.unity3d.com/questions/29346/lock-object-position-in-editor.html
Your answer
Follow this Question
Related Questions
How can I access the object that the inspector is currently displaying? 0 Answers
Scene selection menu 1 Answer
Select Element in ScrollView of EditorWindow 1 Answer
How can I select object in hierarchy in Unity via script? 3 Answers
How to display currently selected GameObject in hierarchy in custom EditorWindow? 2 Answers