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 itscoolbro · Sep 18, 2013 at 04:04 PM · c#instantiatemonobehaviourcard

Using a non-monobehaviour class file

I'm working on a trading card game. This is my cardData class (simplified):

 public class cardData : MonoBehaviour {
     
     public string cardName;
     public int cardManaCost;
     public int cardHp;
     public int cardAttack;
     
     
 }

I have a function that creates a card by cloning the card prefab that contains the above script.

 Card createCard(int n){
   clone the prefab;
   pick the data for the n-th card in a List that contains the data for all the cards;
   assign the right texture for the card;
   return the card;
 }

So as you can see at step 2 in the "createCard()" function, I want to have a list with the data for all the cards. So I created another class "cardData2" that is the same as "cardData" displayed above ^. I want to create objects of this class and put them in a List.

My question is: how do I create instances of this class? I can't work with Monobehaviour because I don't want to attach this script to any game object. Or do I?

I'm confused, and I'm looking for the best solution here but also looking to understand how Unity works with non-monobehaviour classes. I can't seem to instantiate this script if it's just in my resources folder. Please let me know what would be the best approach in your oppinion.

Thanks

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

1 Reply

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

Answer by Jamora · Sep 18, 2013 at 04:29 PM

As the MonoBehaviour-name implies, they are behaviors, not data classes. If your CardData (classes are normally capitalized in C#) does not actually have any impact on the GameObject or the game world at large, then it is better to not extend MonoBehaviour.

You can work with non-MonoBehaviour classes in Unity as you would in any software development: normal classes deriving from System.Object. The only difference to a MonoBehaviour class is that you cannot add them to GameObjects as they are not Components.

You should never be in a position where you absolutely need to have two exactly same classes, differing only in one variable or a few lines of code. If you sometimes do find this is the case, then there is something wrong with your design and you need to sit down and think where the problem is.

To answer your (only answerable) question, you create instances of non-MonoBehaviour classes like you do any normal class, with the new-keyword. The other part of your question is a design decision that only you can answer: Does the class serve any purpose as a behavior on an object in your game? If the answer is in the negative, then don't add it, if on the affirmative, then add it.

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 itscoolbro · Sep 18, 2013 at 04:53 PM 0
Share

So if I have a CardData class in my resources folder (not extending $$anonymous$$ono), can I just instantiate it using "new" ?

Does that mean that Unity sees "CardData" it will start looking for it in all the scripts in my assets folder? Or do all the scripts get run all the time?

avatar image Jamora · Sep 18, 2013 at 06:00 PM 0
Share

First of all, I need to point out that $$anonymous$$ono is a software platform that allows developers to easily create cross platform applications. This shouldn't be confused with $$anonymous$$onoBehaviour (often abbrevioated as $$anonymous$$B), which is the base class for all user scripts attachable to GameObjects.

Well, all C# and UnityScript classes are in an assembly, which is then compiled into a machine readable version so you can run the game. The class need not be in a resources folder (might be better if it weren't), as long as it is under the Assets folder in the project. As long as it's there it will be found by your IDE I assume you're using $$anonymous$$onoDevelop)

Only scripts that are loaded in memory can be considered 'active'. This is sort of misleading, because no object is ever 'inactive'; as that would mean it exists somewhere else than the memory. No script gets 'run' by default. Unity has a system, through which they can call certain methods in each class extending $$anonymous$$onoBehaviour each frame. This is a Unity specific feature, however.

I feel a lot of this confusion arises because you've learned program$$anonymous$$g first with Unity. You should look into more general (i.e. normal) program$$anonymous$$g to fully understand program$$anonymous$$g with Unity.

So the answers are yes, yes, no.

avatar image itscoolbro · Sep 18, 2013 at 06:26 PM 0
Share

Huge thanks for the explanation, that was a huge help.

As for your last point, it's not true but I just understood how Unity works from that remark. In standard program$$anonymous$$g you can have an isolated class in your project that will never be used
if it's not instantiated in another file that is somehow chained to the main() function.

If I understand correctly, it's the same with Unity, I can have a script file that will never be used unless I attach it to a gameObject or instantiated it with "new"?

That makes sense, thanks a lot again :)

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

16 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

Related Questions

Instantiate for few seconds 1 Answer

Accessing Script on instated prefab 1 Answer

Threads for Instantiate 1 Answer

How to create GameObject without adding it to scene? 1 Answer

Multiple Cars not working 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