AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning

I’m attempting to create a “game instance” singleton. Things have been working fine for a while, and still are, but I notice a warning that I either didn’t notice was there before or is new. (I suspect I just didn’t notice it before). Again things seem like they are working, but I don’t like this warning sitting around in the console taunting me. What am I doing wrong?


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()
TableObject:.ctor()
Game:.ctor()
UnityEngine.GameObject:AddComponent()
Game:get_Instance() (at Assets/Scripts/Game.cs:28)
Commodity:Awake() (at Assets/Scripts/Commodity.cs:15)


Note:
Commodity:Awake() is doing this:
Game.Instance.AddCommodity(this); //adds the commodity to a List property on Game

Game.Instance:

public class Game : MonoBehaviour {
	
	
	// GAME INSTANCE *********************************************
	private static Game instance;
	public static Game Instance
	{
		get
		{
			// Unity destroys objects in random order (or so says the internet).
			// This makes sure we don't accidentally create it again if something asks for it after it's been destroyed (which leaves a ghost object in the scene after hitting the stop play button)
			if (applicationIsQuitting) {
				Debug.LogWarning("Game Instance already destroyed on application quit. Won't create again - returning null.");
				return null;
			}
 
			// If the instance doesn't exist yet, make it.
			if(instance == null)
			{
				Debug.Log("Creating new Game Instance.");

				//this generates a warning:
				instance = new GameObject("Game").AddComponent<Game>();

				GameObject.DontDestroyOnLoad(instance);
			}
			
			
			// return the instance
			return instance;
			
			
		}
	}
			
	// Sets the instance to null when the application quits
	private static bool applicationIsQuitting = false;
	public void OnApplicationQuit()
	{ 
		applicationIsQuitting = true;
		instance = null; 
	
	}
	//*************************************************************

//... other stuff

I also tried changing this “instance = new GameObject("Game").AddComponent();” to:

				GameObject newGO = new GameObject();
				newGO.name = "Game";
				newGO.AddComponent<Game>();
				instance = newGO.GetComponent<Game>();

But I get the same error at newGo.AddComponent(); Which suggests AddComponent is doing something that is triggering that warning. Or something in Game class is causing the problem… let me know folks want me to post the whole class (it’s kind of long). I’m hoping just the singleton implementation part is all that is needed to help spot the problem.

I’m at a loss. Especially since things appear to be working just fine. I get a new object called “Game” in the scene, and it has a Game component on it with all the variables I’m setting on it externally via and properties like Game.Instance.XXX

Any help is greatly appreciated.

Thanks! :slight_smile:

Figured it out. I had a property to a MonoBehavior elsewhere in the Game class. (A TableObject) which I declared incorrecly using new.

Like so:
TableObject StartingSupplies = new TableObject()

So the warning was actually about this line, but it was saying it was about the AddComponent(); line.

Hopefully this self-answered question will help others if they are in a similar situation and confused by a similar issue. (Double clicking the warning took me to the line with the AddComponent rather than the actual problem which was incorrectly creating the TableObject with “new”.

Thanks all. :slight_smile:

Please ignore this answer, thanks!