not allowed warning C#

not sure what to say here so ima put the script and the warning and hopefully someone can tell me how to fix it

ItemClass Script

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

public class ItemClass{
    //Icons
    public static Texture2D pistolIcon;

    //Items
    public ItemCreatorClass pistol = new ItemCreatorClass(0,"Pistol", pistolIcon,"Nice little pistol");

    public class ItemCreatorClass{
        public int itemId;
        public string itemName;
        public Texture2D itemIcon;
        public string itemDescription;

        public ItemCreatorClass(int id,string name,Texture2D icon,string description){
            itemId = id;
            itemName = name;
            itemIcon = icon;
            itemDescription = description;
        }
    }
}

Warning Message in console

You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
ItemClass:.ctor()

and here is how i am calling it in the other script

calling from another script

ItemClass itemClass = new ItemClass();

You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

3 Likes

I would just delete the ': MonoBehaviour from line 5. Its not like you are using any features from MonoBehaviour.

tried that no change also tried inheriting from SritableObject but that creates errors

You probably didn’t try it properly. Care to post code again.

updated the post with my current script

You shouldn’t be getting the same error on the same line anymore. Are you trying to use new to create MonoBehaviours in other places

nope i got it fixed but thx anyways