Skip to content
TheChatArchiveThe Chat Archive
Coherecommand-a-03-2025· CohereHugging Face LMArena Human Preference 140K

I want to fill scene in LevelSO by selecting scene or drag/drop into the field

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Mistralmistral-small-3.1-24b-instruct-2503’s answer.
  1. User

    I want to fill scene in LevelSO by selecting scene or drag/drop into the field

  2. Cohere Assistant

    To allow filling the sceneName field in LevelSO by either selecting a scene from a dropdown or dragging/dropping a scene asset directly into the field, we can use Unity's Scene Asset type and custom property drawers. Here's how to implement this:

    1. Update LevelSO to Use SceneAsset

    First, modify the LevelSO script to use SceneAsset instead of a plain string for the scene reference:

    // LevelSO.cs
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    [CreateAssetMenu(fileName = "Level_", menuName = "Level System/Level Data")]
    public class LevelSO : ScriptableObject, ILevelInfo
    {
        [SerializeField] private string levelId;
        [SerializeField] private string displayName;
        [SerializeField] private SceneAsset sceneAsset; // Use SceneAsset instead of string
        [SerializeField] private LevelDifficulty difficulty;
    
        public string LevelId => levelId;
        public string DisplayName => displayName;
        public string SceneName => sceneAsset != null ? sceneAsset.name : ""; // Convert SceneAsset to scene name
    
        public enum LevelDifficulty
        {
            Easy,
            Medium,
            Hard
        }
    }
    

    2. Create a Custom Property Drawer for SceneAsset

    To ensure the SceneAsset field is displayed correctly in the Inspector, Unity already provides a default property drawer for SceneAsset. However, if you want to customize it further or ensure compatibility, you can create a simple editor script:

    // SceneAssetDrawer.cs
    #if UNITY_EDITOR
    using UnityEditor;
    using UnityEngine;
    
    [CustomPropertyDrawer(typeof(SceneAsset))]
    public class SceneAssetDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);
    
            // Draw the default SceneAsset selector
            EditorGUI.BeginChangeCheck();
            var sceneAsset = EditorGUI.ObjectField(position, label, property.objectReferenceValue, typeof(SceneAsset), false) as SceneAsset;
            if (EditorGUI.EndChangeCheck() && sceneAsset != property.objectReferenceValue)
            {
                property.objectReferenceValue = sceneAsset;
            }
    
            EditorGUI.EndProperty();
        }
    }
    #endif
    

    However, since Unity already handles SceneAsset correctly in the Inspector, the above script is optional. The default behavior will allow you to:

    1. Drag and Drop: Drag a scene asset from the Project window directly into the Scene Asset field.
    2. Select from Dropdown: Click the circle to the right of the field to select a scene from the list of available scenes in your project.

    3. Inspector Usage

    With these changes, the LevelSO Inspector will now display a Scene Asset field where you can:

    • Drag and drop a .unity scene file from the Project window.
    • Use the object picker to select a scene from the list of available scenes in your project.

    4. Accessing the Scene Name

    The SceneName property in LevelSO converts the SceneAsset to a scene name string, which can be used with SceneManager for loading:

    // Example usage in LevelLoader.cs
    public class LevelLoader : MonoBehaviour, ILevelLoader
    {
        public void LoadLevel(string levelId)
        {
            var levelData = GetLevelData(levelId);
            if (levelData == null) return;
    
            // Load the scene using the scene name from SceneAsset
            SceneManager.LoadSceneAsync(levelData.SceneName, LoadSceneMode.Additive);
        }
    
        // ... other methods
    }
    

    Final Notes:

    • Scene Asset: SceneAsset is a built-in Unity type that represents a scene file. It automatically handles the correct file type and provides a user-friendly interface in the Inspector.
    • Scene Name: The SceneName property extracts the scene name from the SceneAsset (e.g., "MyScene" from "Assets/Scenes/MyScene.unity").

    This approach ensures a clean and user-friendly way to manage scene references in your level data Scriptable Objects.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.