Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Eversir · Apr 15, 2015 at 12:52 PM · monobehaviouraccessscope

"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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eversir · Apr 15, 2015 at 01:37 PM 0
Share

Thank you - the bottom solution worked straight away. Bit of a noob error, admittedly...

avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eversir · Apr 15, 2015 at 01:19 PM 0
Share

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.

avatar image YoungDeveloper · Apr 15, 2015 at 01:22 PM 0
Share

The error pretty much tells you the problem. Have you even defined variable G$$anonymous$$ in you BetaProperties script?

avatar image Eversir · Apr 15, 2015 at 01:43 PM 0
Share

Yes you were right, thank you. Wood for the trees...

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Google Drive Plugin - full access from android device 1 Answer

Acces monobehaviour script from editor script 2 Answers

Access local method variable in derived class 2 Answers

Get A Variable from another Script in Unity iPhone 2 Answers

How to reference an EditorWindow script in a MonoBehaviour script OR how to pass variables from a EditorWindow to a MonoBehaviour script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges