- Home /
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
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.
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?
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.
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
Follow this Question
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