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 Suudsu2200 · Feb 02, 2013 at 06:31 AM · objectsinstanceclasses

Instances of Classes

Newbie here. I'm trying to make a shell of an RTS. I have a unit script that I attach to each unit that contains that unit's individual properties (HP, etc), and that is attached to each individual unit. I have another script that is attached to no individual unit that takes care of actions pertaining to all units. However, I would like to make a script to pertain to only some units. It would be a "battalion" script that would group together and work with, say, four units as time. My instinct for making this would be to make a script called battalion and to create a new instance of it, once for every group of four units, and have this script attached to no individual unit. However, I'm seeing a lot about how calling constructors on scripts is something you really should not do in Unity, so I'm confused as to how to proceed. Could someone help out?

Comment
Add comment · Show 1
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 robertbu · Feb 02, 2013 at 07:50 AM 0
Share

As @Loiuzein mentions, you can create classes not derived from $$anonymous$$onoBehaviour and use constructors in a standard C# way, but I usually find it easiest to attach a script like your "battalion" script to an empty game object. I can make it into a prefab and create battalions by calling Instantiate().

1 Reply

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

Answer by Loius · Feb 02, 2013 at 06:48 AM

There are no 'scripts' in Unity (as opposed to say Python). Anything that doesn't declare a class in UnityScript is automatically declared as a class (using the file's name), and it extends MonoBehaviour. Anything that doesn't declare a class in C# just won't compile.

You can't use a constructor of a an object which extends MonoBehaviour. If you create your own class extending "not MonoBehaviour", you can call the constructor all you want. You have to use AddComponent for Component-type classes because Unity needs to do behind-the-scenes stuff to get everything working for you.

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

10 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

Related Questions

Calling a function across all instances of an object 2 Answers

Build Queue Logic In Turn-based Strategy 1 Answer

Cannot get variable from another class. Error: NullReferenceException 2 Answers

How to select a class instance from the inspector? 1 Answer

How to access an instance of a class when the class isn't known 2 Answers


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