Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by MarkD · Oct 09, 2015 at 12:59 PM · editor-scriptingguilayoutfieldobjectfield

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.

alt text

7f575b020d967ac64485d54b63c3e66c.png (2.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkD · Oct 09, 2015 at 02:47 PM 0
Share

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.

alt text

alt text

7f575b020d967ac64485d54b63c3e66c.png (2.0 kB)
006e33fe0af36f8f4220d0d206b02b44.png (1.1 kB)
avatar image Garazbolg MarkD · Oct 09, 2015 at 03:17 PM 0
Share

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.

avatar image MarkD · Oct 09, 2015 at 03:27 PM 0
Share

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.

avatar image lighting MarkD · Feb 22, 2016 at 11:20 PM 0
Share

Hi. I have discovered the idea with this EditorGUILayout.ObjectField. Are you still interested in that ?

avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom EditorWindow Scrollbars not working with GUILayout areas 1 Answer

Editor GUI ScrollView Actual Scrollbar Not Showing Up?? 1 Answer

ApplyModifiedProperties seems not to work with AllowSceneObjects = true 1 Answer

EditorGUILayout.ObjectField does not allow me to select a script. 1 Answer

How To Best Match Editor GUI Content Heights 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges