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 wikarus · Aug 28, 2013 at 12:59 AM · c#instantiateclass

Instantiate class from other script but same gameObject

I have two scripts in my Main Camera:

     public class defaultBehavior : MonoBehaviour {
         void Start () {    
             messages msg = new messages();
              msg.isMsgEnabled = true;
             msg.showMessage("teste");
         }
         void Update () {
         }
     }

and

 public class messages : MonoBehaviour {
     
     public bool isMsgEnabled = false;
     
     public void showMessage(string message) {
         if(isMsgEnabled) {
             Debug.Log (message);
         }
     }
     
     
 }

From defaultBehavior, how can I access my messages class, instantiate the isMsgEnabled bool and call the showMessage() void?

I've tried to instantiate with the new keyword but Unity throw me the following alert:

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() messages:.ctor()

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
0
Best Answer

Answer by getyour411 · Aug 28, 2013 at 01:31 AM

Try this link, expand Section 1 - Accessing methods and variables

http://unitygems.com/mistakes1/

Comment
Add comment · Show 2 · 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 wikarus · Aug 28, 2013 at 02:21 AM 0
Share

Thanks @getyour411 your link helped a lot!

avatar image nadavrt · Oct 08, 2019 at 01:23 PM 0
Share

You should remove that link. I don't know where it originally pointed, but it is now pointing to a phishing site.

avatar image
0

Answer by Azmodii · Aug 28, 2013 at 02:19 AM

Does the script need to inherit MonoBehaviour?

Try removing;

 : MonoBehaviour

From the class declaration.

You can then access it via;

 messages _messageClass = new messages();
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
avatar image
0

Answer by TrickyHandz · Aug 28, 2013 at 01:51 AM

The messages class should not inherit from MonoBehaviour if you want to instantiate it. Also, you should add a constructor to handle everything you want to happen when a new instance of the class is created. Like this:

 using UnityEngine;
 
 public class messages
 {
     public bool isMsgEnabled;
 
     // Constructor
     public messages()
     {
             // Set isMsgEnabled to true
             // when a new instance is created
         isMsgEnabled = true;
     }
 
     public void showMessage(string message)
     {
         if (isMsgEnabled)
         {
             Debug.Log(message);
         }
     }
 
 }

Next, your defaultBehavior script can be modified like so:

 using UnityEngine;
 
 public class defaultBehavior : MonoBehaviour
 {
     messages msg;
 
     void Start()
     {
         messages msg = new messages();
         msg.showMessage("test");
     }
     void Update()
     {
     }
 }

Comment
Add comment · Show 2 · 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 wikarus · Aug 28, 2013 at 02:09 AM 0
Share

I did this but now i'm receiving this message:

The class defined in the script file named 'messages' is not derived from $$anonymous$$onoBehaviour or ScriptableObject!

That doesn't means that messages should extend something?

avatar image TrickyHandz · Aug 28, 2013 at 02:42 AM 0
Share

If you already attached an instance of "messages" to a GameObject as a component when you removed the inheritance of $$anonymous$$onoBehaviour, you'll get that message. Simply remove the "messages" component from the GameObject and it will fix it.

All you need to use is the reference to the msg property on the defaultBehavior component to do all the work with it.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Behind the scenes voodoo w/ Instantiate 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

C# Utility Class 1 Answer

The Same Declaration in Different Classes? 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