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
I am trying to connect these two scripts and I don’t know which one this error is for. The full error is:
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()
inventoryV1:.ctor()
Here are the two scripts
sing UnityEngine;
using System.Collections;
public class ItemClass : MonoBehaviour
{
//DECLARE ITEMS HERE
//MUST BE PUBLIC
//DECLARE ICONS HERE
//MUST BE STATIC AND PUBLIC
public class ItemCreatorClass
{
string name;
string description;
int id;
int damage;
int speed;
int weight;
int hungerEffect;
int thirstEffect;
int stackAmount;
Texture2D icon;
public ItemCreatorClass(int ide,string nam, string des, int dam, int spe, int wei, int hun, int thi, int sta, Texture2D ico)
{
id = ide;
name = nam;
description = des;
damage = dam;
speed = spe;
weight = wei;
hungerEffect = hun;
thirstEffect = thi;
stackAmount = sta;
icon = ico;
}
}
}
using UnityEngine;
using System.Collections.Generic;
public class inventoryV1 : MonoBehaviour {
public static bool inventoryOn = false;
//GUI FONT AND SIZE
private Vector2 windowPosition = new Vector2(300,0);
private Vector2 windowSize = new Vector2(745, 750);
private Vector2 closePosition = new Vector2(705,9);
private Vector2 closeSize = new Vector2(24,24);
private Vector2 inventoryTitlePosition = new Vector2(245,11);
private Vector2 inventoryTitleSize = new Vector2(125,50);
private Vector2 playerTitlePosition = new Vector2(17,11);
private Vector2 playerTitleSize = new Vector2(100,50);
private Vector2 craftingTitlePosition = new Vector2(17,414);
private Vector2 craftingTitleSize = new Vector2(125,40);
//Dictionary
static public Dictionary<int, string> inventoryNameDictionary;
//WINDOW TEXTURES
public Texture inventoryWindow;
//BUTTON TEXTURES
public Texture closeInventory;
public Texture inventorySlot;
//ACCESS CLASSES
ItemClass itemObject = new ItemClass();
void Update ()
{
//ON OR OFF
if (Input.GetKeyUp (KeyCode.Tab))
{
if (inventoryOn == false)
{
inventoryOn = true;
}
else if (inventoryOn == true)
{
inventoryOn = false;
}
}
}
//GUI AND FUNCTIONALITY
void OnGUI()
{
if (inventoryOn == true)
{
GUILayout.BeginArea(new Rect (windowPosition.x, windowPosition.y, windowSize.x, windowSize.y), inventoryWindow);
//Close Button
if (GUI.Button (new Rect (closePosition.x, closePosition.y, closeSize.x, closeSize.y), closeInventory)) {
inventoryOn = false;
}
//INVENTORY SECTION TITLES
GUI.Label(new Rect(inventoryTitlePosition.x, inventoryTitlePosition.y, inventoryTitleSize.x, inventoryTitleSize.y),"Inventory");
GUI.Label (new Rect( playerTitlePosition.x, playerTitlePosition.y, playerTitleSize.x, playerTitleSize.y), "Player");
GUI.Label(new Rect(craftingTitlePosition.x, craftingTitlePosition.y, craftingTitleSize.x, craftingTitleSize.y), "Crafting Menu");
//INVENTORY SLOTS
//FIRST ROW
GUI.Button (new Rect (270, 50, 70, 70), inventoryNameDictionary[0]);
GUI.Button (new Rect (345, 50, 70, 70), inventoryNameDictionary[1]);
GUI.Button (new Rect (420, 50, 70, 70), inventoryNameDictionary[2]);
GUI.Button (new Rect (495, 50, 70, 70), inventoryNameDictionary[3]);
GUI.Button (new Rect (570, 50, 70, 70), inventoryNameDictionary[4]);
GUI.Button (new Rect (645, 50, 70, 70), inventoryNameDictionary[5]);
//SECOND ROW
GUI.Button (new Rect (270, 125, 70, 70), inventoryNameDictionary[6]);
GUI.Button (new Rect (345, 125, 70, 70), inventoryNameDictionary[7]);
GUI.Button (new Rect (420, 125, 70, 70), inventoryNameDictionary[8]);
GUI.Button (new Rect (495, 125, 70, 70), inventoryNameDictionary[9]);
GUI.Button (new Rect (570, 125, 70, 70), inventoryNameDictionary[10]);
GUI.Button (new Rect (645, 125, 70, 70), inventoryNameDictionary[11]);
//THIRD ROW
GUI.Button (new Rect (270, 200, 70, 70), inventoryNameDictionary[12]);
GUI.Button (new Rect (345, 200, 70, 70), inventoryNameDictionary[13]);
GUI.Button (new Rect (420, 200, 70, 70), inventoryNameDictionary[14]);
GUI.Button (new Rect (495, 200, 70, 70), inventoryNameDictionary[15]);
GUI.Button (new Rect (570, 200, 70, 70), inventoryNameDictionary[16]);
GUI.Button (new Rect (645, 200, 70, 70), inventoryNameDictionary[17]);
//FOURTH ROW
GUI.Button (new Rect (270, 275, 70, 70), inventoryNameDictionary[18]);
GUI.Button (new Rect (345, 275, 70, 70), inventoryNameDictionary[19]);
GUI.Button (new Rect (420, 275, 70, 70), inventoryNameDictionary[20]);
GUI.Button (new Rect (495, 275, 70, 70), inventoryNameDictionary[21]);
GUI.Button (new Rect (570, 275, 70, 70), inventoryNameDictionary[22]);
GUI.Button (new Rect (645, 275, 70, 70), inventoryNameDictionary[23]);
GUILayout.EndArea ();
//ARMOR SLOTS
//DICTIONARY
inventoryNameDictionary = new Dictionary<int, string>()
{
//THE LINE BENEATH THIS COMMENT HAS THE ERROR
{0, string.Empty},
{1, string.Empty},
{2, string.Empty},
{3, string.Empty},
{4, string.Empty},
{5, string.Empty},
{6, string.Empty},
{7, string.Empty},
{8, string.Empty},
{9, string.Empty},
{10, string.Empty},
{11, string.Empty},
{12, string.Empty},
{13, string.Empty},
{14, string.Empty},
{15, string.Empty},
{16, string.Empty},
{17, string.Empty},
{18, string.Empty},
{19, string.Empty},
{20, string.Empty},
{21, string.Empty},
{22, string.Empty},
{23, string.Empty}
};
}
}
}
As a side note, there is a non-generic form of
– HoeloeAddComponent, which doesn't use the triangle brackets notation, and instead takes the class name as a string. I would strongly recommend using generics wherever possible - they are far tidier, potentially faster, and easier for the debugger to find problems with.For some reason even though I declared the obj public, it won't show it in the inspector. Any help with why that is happening?
– NutellaDaddyA number of things could be causing that, one would be that it is not of a type that Unity can serialise (if its type is
– HoeloeGameObject, this won't be a problem), another is that it has to be defined at the class level, not inside a method. It doesn't have to be defined there, but it has to at least be declared at that point. Another likely solution is that your code has compiler errors (that is, errors that are preventing the code from compiling, not just from running, check the console window), which will prevent it from updating the script.Thank you. It is probably because I didn't use the actual values for the Items
– NutellaDaddyI think a more convenient way is to remove the
– ocimumMonobehaviourextension. To add a script to a game object just to go the unity way without needing the script in the object sounds confusing to me. But thanks for your explanation, now I understand why the warnings came! :)