Issues with scripted object

whenever i start unity and load my project this happens:

i dont have any errors when opening unity up…

Iron ore is a scripted object

its part of the MaterialItem class which inherits from BasicItem → ScriptableObject

[System.Serializable]
public abstract class BasicItem : ScriptableObject
{
    [SerializeField] protected string ItemName;
    [SerializeField] [TextArea] protected string ItemDescribtion;
    [SerializeField] protected int ItemID;
    [SerializeField] protected Sprite ItemSprite;
    [SerializeField] protected int BasePrice;
    [SerializeField] protected List<BasicItem> IngridientList = new List<BasicItem>();

    public string GetItemName()
    {
        return ItemName;
    }
    public string GetItemDescribtion()
    {
        return ItemDescribtion;
    }
    public int GetItemID()
    {
        return ItemID;
    }
    public Sprite GetItemSprite()
    {
        return ItemSprite;
    }
    public int GetItemBasePrice()
    {
        return BasePrice;
    }
    public List<BasicItem> GetItemIngridientList()
    {
        return IngridientList;
    }
}

[CreateAssetMenu(fileName = "NewCatchItem", menuName = "NewItem/CatchItem")]
[System.Serializable]
public class CatchItem : BasicItem
{
    [SerializeField] protected int CatchPower;
    [SerializeField] protected GameObject SpriteForCatching;

    public GameObject GetSpriteForCatching()
    {
        return SpriteForCatching;
    }
    public int GetCatchPower()
    {
        return CatchPower;
    }
}

[CreateAssetMenu(fileName = "NewMaterialItem", menuName = "NewItem/MaterialItem")]
[System.Serializable]
public class MaterialItem : BasicItem 
{

}

the weird thing is the code works fine to solve the issue in the image above i can do the following:
i add one empty line and save ;(

sfdfddfssdf

what can i do that it just loads properly whenever i open unity?

For both ScriptableObject and MonoBehaviours, they need to be in separate script files per class. Can’t have multiple in the same script asset. You also need to make sure the script asset name matches the class name inside.

So you need to move MaterialItem into its own script file here.

2 Likes

but enums are fine ?

The rule only applies to your scriptable objects and monobehaviours classes.

The exception would be abstract ones, as you can’t make actual instances of those, of course.

Everything else, enums, regular C# classes, etc, you can have any many as you want in one file, even nested in other types.

2 Likes

one more thing

i created new scripts for the diffrent item types and the items now load properly :slight_smile: thx for that

i do assing some of the items in the inspector but the assingment is removed when i open unity again

can i do something about that?

Is this still happening after moving these scriptable object types into their own script files now?

yes it still happens

i tested it now by closing and reopening unity twice
i was to quick about it still not working properly

CatchItem:

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

[CreateAssetMenu(fileName = "NewCatchItem", menuName = "NewItem/CatchItem")]
[System.Serializable]
public class CatchItem : BasicItem
{
    [SerializeField] protected int CatchPower;
    [SerializeField] protected GameObject SpriteForCatching;

    public GameObject GetSpriteForCatching()
    {
        return SpriteForCatching;
    }
    public int GetCatchPower()
    {
        return CatchPower;
    }
}

BasicItem

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

[System.Serializable]
public abstract class BasicItem : ScriptableObject
{
    [SerializeField] protected string ItemName;
    [SerializeField] [TextArea] protected string ItemDescribtion;
    [SerializeField] protected int ItemID;
    [SerializeField] protected Sprite ItemSprite;
    [SerializeField] protected int BasePrice;
    [SerializeField] protected List<BasicItem> IngridientList = new List<BasicItem>();

    public string GetItemName()
    {
        return ItemName;
    }
    public string GetItemDescribtion()
    {
        return ItemDescribtion;
    }
    public int GetItemID()
    {
        return ItemID;
    }
    public Sprite GetItemSprite()
    {
        return ItemSprite;
    }
    public int GetItemBasePrice()
    {
        return BasePrice;
    }
    public List<BasicItem> GetItemIngridientList()
    {
        return IngridientList;
    }
}

maybe becouse basic item is abstract ?

BasicItem being abstract is fine.

Have you made sure the CatchItem’s file name matches the class name?

You may have to assign the script to the asset’s monoscript field again as well.

yes the file name of the CatchItem class is CatchItem

i tryed to fix everything by hand again with my add a empty line and save trick… then i assigned everything again

saved unity
closed unity
reloaded my project

some of the files still dont load properly

weird detail i noticed:

it keeps the assigned ingridients intact when finaly(after adding empty lines) loading the material

while on the game assets like the npc the assignment is dropped and has to be reassigned
(item sprite was never assigned)

This might be a point where you first: clear the Library folder. If that doesn’t work, do a Reimport All.


sadly didnt work

first i made a backup
then i removed the libary folder

  • some of the items still didnt load but i also only had the main camera left

so i removed the project and loaded the backup
then i did the reimport all
result in the screen at top

what i still can do is to update unity to a diffrent unity version and see if that may helps

update to unity 6 did also not resolve this issue :frowning:

MaterialItem.cs

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

[CreateAssetMenu(fileName = "NewMaterialItem", menuName = "NewItem/MaterialItem")]
[System.Serializable]
public class MaterialItem : BasicItem
{

}