Home > front end >  Problem with ScriptableObject. Check that the definition is in a file of the same name and that it c
Problem with ScriptableObject. Check that the definition is in a file of the same name and that it c

Time:06-03

I can´t do an ScriptableObject in unty. I do this script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[CreateAssetMenu(fileName = "SpriteDB", menuName = "AR GPS/SpriteDB")]
public class SpriteDB : ScriptableObject
    {
        
        public class Iconos{
            
            public string nombre;
            public Sprite icon;
            public string url;
        }
        public List<Iconos> Entries;
        public Sprite GetEntryById(string Id)
        {
            Sprite result = null;

            foreach (var entry in Entries)
            {
                if (entry.nombre == Id)
                {
                    result = entry.icon;
                    break;
                }
            }

            return result;
        }

    }

Butwhen i create the object i get the following error:No script asset for SpriteDB. Check that the definition is in a file of the same name and that it compiles properly. filesenter image description here

CodePudding user response:

The name of the csharp file needs to match the class name exactly. And it looks like your cs file is called spriteDB.cs while your class is called SpriteDB. Note the capital S. Rename your cs file to SpriteDB.cs and Unity will stop complaining.

CodePudding user response:

Name of MonoBehavior or ScriptableObject class must match with .cs file name in Unity as @frankhermes said. Char case matters!

Array in not database)

// only Serializable types of class and struct can by display in inspector
[Serializable]
public struct IconData
{
    public string ID;
    public string URL;
    public Sprite Sprite;
}

[CreateAssetMenu(fileName = "ItenDataCollection", menuName = "ItenData/Collection")]
public class ItenDataCollection : ScriptableObject
{
    // hello encapsulation, field must be privat
    // SerializeField for serialize privat field (public serialize automatically)
    [SerializeField] private IconData[] _collection;

    public Sprite GetSprite (string id)
    {
        return _collection.FirstOrDefault(i => i.ID == id).Sprite;
    }
}
  • Related