- Home /
"You are trying to create a MonoBehaviour using the 'new' keyword.
This isn't exactly a new issue but the solutions to other questions were written some time ago. Either they don't work in Unity 5, or I am still making a mistake somewhere.
I have a GameManager script, and some other scripts for various game objects (e.g. BetaProperties). The scripts attached to these gameObjects need to access and modify information in the GameManager script using the following:
GameManager myGameManager = new GameManager ();
and then referencing it where appropriate, e.g.:
myGameManager.CurrentTemp = myGameManager.CurrentTemp + 50;
Both the GameManager and BetaProperties classes open up with this respectively:
public class GameManager : MonoBehaviour {
public class BetaProperties : MonoBehaviour {
But I get an error:
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 gather this is because I have used MonoBehaviour in both scripts, and one is trying to access the other. But if I remove MonoBehaviour from either script, my code will not work. I think I need to change this bit:
GameManager myGameManager = new GameManager ();
to something like:
GameManager myGameManager = obj.AddComponent<GameManager> ();
...but it still doesn't work (Error CS0103: The name 'obj' does not exist in the current context)
Answer by _joe_ · Apr 15, 2015 at 01:27 PM
To initialize the variable you can do either:
GameManager myGameManager = GameObject.Find("GM").GetComponent<GameManager> ();
or simply create a public variable in your class:
public GameManager myGameManager;
And drag and drop GM from the hierarchy to the inspector of your script.
Thank you - the bottom solution worked straight away. Bit of a noob error, admittedly...
Answer by YoungDeveloper · Apr 15, 2015 at 12:55 PM
Classes which inherit from Monobehaviour can't be instantiated using new keyword.
public class GameManager : MonoBehaviour {
}
GameManager myGameManager = new GameManager (); //you can't do this
Instead, you should threat it as component and only add it.
someGameObject.AddComponent<GameManager>();
In your case, obj is some gameObject in the scene. You should set it before adding component, otherwise it will throw null reference exception.
O$$anonymous$$, so following on from this I have a game object called G$$anonymous$$. $$anonymous$$y Game$$anonymous$$anager script is attached to this. I used the following code in my BetaProperties script:
Game$$anonymous$$anager myGame$$anonymous$$anager = G$$anonymous$$.AddComponent<Game$$anonymous$$anager> ();
But it throws error CS0103: The name 'G$$anonymous$$' does not exist in the current context.
Am I going about it in the right way, or did you ins$$anonymous$$d mean to use it like this:
G$$anonymous$$.AddComponent<Game$$anonymous$$anager>("CurrentTemp");
Assu$$anonymous$$g CurrentTemp is the thing I need from my Game$$anonymous$$anager script.
Thank you.
The error pretty much tells you the problem. Have you even defined variable G$$anonymous$$ in you BetaProperties script?
Answer by Hrungdak · Apr 15, 2015 at 12:56 PM
obj has to be some GameObject, like your camera or player. I for myself would attach the GameManager to the camera, but every empty Gameobject fulfills the task.