Search Unity

Can't add audio clip to custom editor

Discussion in 'Scripting' started by Josiah_Ironclad, Jan 21, 2020.

  1. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    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);
     
    Last edited: Jan 21, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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);
     
  3. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    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.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    So...add a cast to the argument that's supposed to be of type Object? (That's the second one.)
     
  5. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    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.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    weaponScriptable.fireSound = EditorGUILayout.ObjectField("Fire sound", (Object)(weaponScriptable.fireSound), typeof(AudioClip), true);
     
  7. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    Tried that already.
     
  8. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    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.

    Code (CSharp):
    1. weaponScriptable.fireSound = (AudioClip)EditorGUILayout.ObjectField("Fire sound", weaponScriptable.fireSound, typeof(AudioClip), false);
     
  9. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    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.

    Code (CSharp):
    1. SerializedProperty fireSound;
    2.  
    3. void OnEnable()
    4. {
    5.     fireSound = serializedObject.FindProperty("fireSound");
    6. }
    7.  
    8. public override void OnInspectorGUI()
    9. {
    10.     EditorGUILayout.PropertyField(fireSound);
    11.    
    12.     serializedObject.ApplyModifiedProperties();
    13. }
     
    Josiah_Ironclad likes this.
  10. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    That works, thanks! :)
     
    SisusCo likes this.
  11. holyfot

    holyfot

    Joined:
    Dec 16, 2016
    Posts:
    42
    Code (CSharp):
    1. fireSound.objectReferenceValue = EditorGUILayout.ObjectField("Fire Sound", fireSound.objectReferenceValue, typeof(AudioClip), true);