- Home /
Set default object EditorGUILayout.ObjectField
Hi,
First of all thanks for reading.
I am writing an editor script with several fields. One of them is an EditorGUILayout.ObjectField. Everything goes right and all editor fields work.
But however, I would like this particular ObjectField to have a GameObject applied by default (through script), not manually through the editor as is done by usual.
This will allow the user to have a default GameObject in the slot, but still gives him the ability to swap it out later.
Thanks for your help.
Answer by Bunny83 · Oct 09, 2015 at 01:26 PM
You should be more specific about what GameObject you talk about. A GameObject instance can only "live" either in the current scene or as prefab asset inside the project. Do you have the desired GameObject instance already somewhere? If so is it a prefab? If that's the case you can simply use the AssetDatabase to load your prefab from the path where you stored the prefab in case the field is "null".
If it's not a prefab but an object in the scene you need some way to identify it (by name / tag / attached component / ...). For that you can use the usual search functions like GameObject.Find / FindObjectOfType / FindGameObjectsWithTag / ...
If you haven't any GameObject instance yet, you can create one, but keep in mind that it will appear in the scene and will be saved along with the scene.
In any of those 3 cases you just want to check if the field is null inside your editor code and if it's null, do your desired action.
For example:
if (myField == null)
myField = new GameObject("temp object");
myField = (GameObject)EditorGUILayout.ObjectField(myField, typeof(GameObject), true);
This would be case 3 where you simply create a new GameObject when the field is null.
You might want to use a hybrid of the cases i mentioned if you want to use an object in the scene. Otherwise your script will create an instance everytime the field is set to null automatically. So if the user selects the object field and presses delete the field is set to null and immediately it creates a new instance, even when there's already a temp object. So you end up with a new instance everytime.
So a better approach is to first search if an instance exists, if so, use it. If not create a new instance and use that.
Hi, thank you for responding.
I already knew what you showed me. I should have been more clear. It is already clear to me how to set a field up correctly. What I want to do for the user, is to have a default GameObject already placed in the field slot itself.
So ins$$anonymous$$d of showing GameObject (empty) it should be showing the given object by default.
//Unity Script
Source.GetComponent(Example).examplel= EditorGUILayout.ObjectField(Source.GetComponent(Example).examplel, GameObject, false);
edit: I already did a check to see if it's empty, but even if I set an object to the slot through script, it is not showing.
So i want the slot filled from the start, ins$$anonymous$$d of being empty at start.
He actually answered your question you just have to affect your field, in your Case :
Source.GetComponent(Example).examplel = (Whatever is your gameObject);
It can either be in your Editor Script before your line or in your Component script as a default value for your field.
It is how it works, if it doesn't your problem may come from the way you acquire your reference to the GameObject and that depends on where it is.
I see, then it must be the way I set up the reference to my GameObject. Because everything was exactly the same (before I posted this question). It just won't show up.
Thanks to both of you.
Hi. I have discovered the idea with this EditorGUILayout.ObjectField. Are you still interested in that ?
Answer by tylerwongj · Apr 12, 2019 at 07:34 AM
If you're trying to get a Prefab from your Assets/ folder to fill the slot, you can use AssetDatabase.LoadAssetAtPath()
.
For example:
GameObject source = (GameObject) AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Levels/Level.prefab", typeof(GameObject));
source = (GameObject) EditorGUILayout.ObjectField(source, typeof(GameObject), true);
Your answer
![](https://koobas.hobune.stream/wayback/20220612040641im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
EditorGUI, EditorGUILayout, GUI, GUILayout... pshhh... WHEN TO USE WHAT?! 3 Answers
Custom Inspector button texture? 1 Answer
Using EditorGUI.ObjectField in PropertyDrawer 0 Answers
Custum editor: How do I setup a 3x3 grid of ObjectFields? 2 Answers
Editor Script for selecting class property for saving 1 Answer