Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Avash · Sep 18, 2015 at 05:13 PM · c#scripting problem

Classes in a main class

I have multiple scripts and I want to put them in one main script so I could remove other components, but how do I make the other script support monobehaviour?

Comment
Add comment · Show 3
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 DiegoSLTS · Sep 18, 2015 at 06:49 PM 0
Share

@Avash You'll have to provide the scripts, it's impossible to answer your question without them.

Or explain your problem with more details, what would be "the other script"? What do you mean by "support" monobehaviour?

avatar image Avash · Sep 18, 2015 at 06:58 PM 0
Share

To use monobehaviour funktions such as Destroy().

avatar image Jessespike · Sep 18, 2015 at 07:26 PM 1
Share

You want a component to contain multiple monobehaviours? Don't think Unity works like that. That goes against the GameObject-Component relationship system.

$$anonymous$$ultiple classes in a script? No problem...

$$anonymous$$ultiple behaviours in a script? Causes problems.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by BMayne · Sep 18, 2015 at 08:00 PM

Hello there,

You have to put each of your class that inherit from Monobahviour in their own file with the same name as the class itself.

Cheers,

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 RudyTheDev · Sep 18, 2015 at 08:40 PM 0
Share

Theoretically, you can ($$anonymous$$ainClass.cs):

 using UnityEngine;
 
 public class $$anonymous$$ainClass : $$anonymous$$onoBehaviour
 {
     private void Start()
     {
         gameObject.AddComponent<InnerClass>();
         gameObject.AddComponent<OtherClass>();
     }
 
     public class InnerClass : $$anonymous$$onoBehaviour
     {
         private void Update()
         {
             Debug.Log("Inner update");
         }
     }
 }
 
 public class OtherClass : $$anonymous$$onoBehaviour
 {
     private void Update()
     {
         Debug.Log("Other update");
     }
 }

But you won't be able to work with them in the editor/inspector. There may be other issues as well, so it's best to avoid it.

avatar image BMayne RudyTheDev · Sep 18, 2015 at 09:23 PM 0
Share

Yes you are physically able too but they will not be loaded by Unity and you will not be able to add them using any of Unity UI. If you try to add the component in code you will get a pop up window from Unity saying "Your script name must much the class name".

avatar image RudyTheDev BMayne · Sep 19, 2015 at 02:28 PM 0
Share

Yes, that's why I said "you won't be able to work with them in the editor/inspector". But they will be "loaded", that is Unity will still (de)serialize the data. You can do:

 using UnityEngine;
 
 [ExecuteInEdit$$anonymous$$ode]
 public class $$anonymous$$ainClass : $$anonymous$$onoBehaviour
 {
     private void Start()
     {
         if (GetComponent<OtherClass>() == null)
             gameObject.AddComponent<OtherClass>().x = 5;
     }
 }
 
 public class OtherClass : $$anonymous$$onoBehaviour
 {
     public int x = 0;
 }

Add $$anonymous$$ainClass, then remove it. Then save and reload the project. It will have saved the value 5.

I am not saying this is a good way to do it. It's a bad way and no one should do it. But it's not impossible.

avatar image
0

Answer by Avash · Sep 19, 2015 at 02:09 PM

So I had some problems with inheriting and now do this: MonoBehaviour.Destroy() or MainClass.transform.parent. I think it's good enought.

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What are the commands to change aspects of a vignette? 1 Answer

Why when creating new animator controller for the character the character is not walking right ? 0 Answers

script with individual function for each gameObject or public script variable to solve inventory system problem 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