Can't add audio clip to custom editor

Very simple, trying to make an AudioClip field pop up in my custom editor, but I’m getting two errors in console.

Editor line:
weaponScriptable.fireSound = EditorGUILayout.ObjectField("Fire sound", weaponScriptable.fireSound);

ScriptableObject variable being referenced:
public AudioClip fireSound;

Errors:
(39,62) "cannot convert from ‘string’ to ‘UnityEngine.Object’ "
(39,76) "cannot convert from ‘UnityEngine.AudioClip’ to ‘System.Type’ "

I looked around int he manual and Scripting API pages, searched for Answers and forum threads, found very little and what I did find wasn’t addressing the issue I’m having.

This is where the errors are pointing:
weaponScriptable.fireSound = EditorGUILayout.ObjectField( ERROR 1 “Fire sound”, ERROR 2 weaponScriptable.fireSound);

I don’t have much experience with custom editors, but it looks like your argument list is incomplete. In this doc the only listed overload for ObjectField that takes a string is

public static Object ObjectField(string label, Object obj, Type objType, bool allowSceneObjects, params GUILayoutOption[ ] options);

Looks like you’ve got the first 2 arguments but you need at least 2 more (and therefore the compiler is trying to match your arguments to a different overload that takes different types). Based on the example, I’d guess you probably want something like

weaponScriptable.fireSound = EditorGUILayout.ObjectField("Fire sound", weaponScriptable.fireSound, typeof(AudioClip), true);

Now I’m getting:
Cannot implicitly convert type ‘UnityEngine.Object’ to ‘UnityEngine.AudioClip’. An explicit conversion exists (are you missing a cast?)

Other fields like “IntField” also have additional parameters, but I never had problems using only two with them.

So…add a cast to the argument that’s supposed to be of type Object? (That’s the second one.)

Sorry, I can’t seem to figure out how. I’ve not done casting much, and I tried a few ways of what I know, but the error just stays there. I’m either trying to cast the wrong thing, in the wrong way, or in the wrong place.

weaponScriptable.fireSound = EditorGUILayout.ObjectField(“Fire sound”, (Object)(weaponScriptable.fireSound), typeof(AudioClip), true);

Tried that already.

EditorGUILayout.ObjectField returns an Object so you need to cast it to the more specific type AudioClip.

You can also set the last parameter allowSceneObjects to false, since AudioClips are asset types and as such can’t exist in the scene.

weaponScriptable.fireSound = (AudioClip)EditorGUILayout.ObjectField("Fire sound", weaponScriptable.fireSound, typeof(AudioClip), false);
3 Likes

Also note that if you want things like undo, multi-editing, prefab value overriding and scene dirtying to work, you should use EditorGUILayout.PropertyField or the variant of EditorGUILayout.ObjectField that takes a SerializedProperty argument.

SerializedProperty fireSound;

void OnEnable()
{
    fireSound = serializedObject.FindProperty("fireSound");
}

public override void OnInspectorGUI()
{
    EditorGUILayout.PropertyField(fireSound);
   
    serializedObject.ApplyModifiedProperties();
}
1 Like

That works, thanks! :slight_smile:

1 Like
fireSound.objectReferenceValue = EditorGUILayout.ObjectField("Fire Sound", fireSound.objectReferenceValue, typeof(AudioClip), true);