- Home /
Understanding Add component and classes.
Hey guys I am trying to get a better under standing of objects.
Does Add component just add a class to a game object? what would be the difference in creating a new class / structure outside and then assigning it to a gameobject at runtime when I need it? (When a user clicks on a game object to see stats, using reference to class in list)
Lets say I want to create a world with 10000 characters and each one has for example a hungry meter. should I add a component to each one that subtracts itself by the time or have 1000 classes that subtract over time and assign the class when I need it?
Just trying to understand best practices.
I'm trying to figure this out right now too. How different objects can contain different stats (hunger, in this example). I'm pretty sure he wants just one class with 1000, or however many, instances of the same class. But, like me, it sounds like the OP isn't sure how to attach the unique class instance/object to a specific thing in Unity...like a hungry character in the OP's case.
Hey Topthink, man this was over a year ago since then I have attended school and understood the answer to my question.
When I asked this question it was more of a question about classes and what to assign to game objects. In this case (having 1000 characters with hunger meters) I would create a class:
public class Character{
public float hunger = 0;
void Character(){
StartHunger();
}
void Eat(myFoodClass food)
{
hunger -= food.calories;
if(hunger < 0)
hunger = 0;
}
void StartHunger()
{
//Start logic to add to hunger
}
}
From here I could either add it to a gameobject: someCharacter.AddComponent(); OR Just keep it as a static class and use it to reference 'someCharacter' in it. For dealing with many classes like this I would just use a ID (its name, or its tag, or some thing similar) on a object like 'Character200' and use it like a hash to call a dictonary.
Dictonary<string, Character> myCharacterDictonary = new Dictonary<string, Character>();
//add all the characters to the myCharacterDictonary
//To access dic with gameobject (Character200)
Food AppleClass = new Food(apple);
myCharacterDictonary[selectedGameobject.name].Eat(AppleClass);
Let me know if you have further questions
Answer by DiegoSLTS · Mar 22, 2015 at 10:29 PM
I don't understand what you're saying, "adding a class to a game object" is not a real thing, what do you mean?
Unity uses a component based development, each object is a collection of components, and each component adds some new things to that object. This way if you want 2 unrelated things to be able to shoot, you just add a "Shooter" component to both.
A component in it's basic form is a structure that holds some data, and the engine knows that if there's a component of that type, the data it contains should be used in a specific way. In the most general case the data contained in a component can also be code, and Unity knows that each time it finds a component with code it might be able to call some defined functions (Awake, Start, Update, etc, etc).
When you Add a component your adding an instance of a Component to the list of components of a game object.
You can add components in the editor in a lot of ways, or you can add components in code when you need to (with the AddComponent method), but those components have to be defined in your project, an assets that contains the script for the component should already exists.
I don't understand the last part about the 10000 characters, again, what do you mean by having "1000 classes that substract over time and assign the class when I need it"? You shouldn't have 1000 classes that do the same, maybe you meant you have 1000 instances of a class. The only thing I can tell you is that you should have components inside gameobjects, having a list of components would be useless and I'm not even sure if that's possible.
Your answer
Follow this Question
Related Questions
Instantiated objects are not destroying 1 Answer
Application.LoadLevel - Leaves behind multiple instances of scripts / objects 0 Answers
Enable/Disable Game objects after Wait For seconds 5 Answers
Orbiting objects appear stretched 1 Answer
How to make object fall slowly but bounce normally? 2 Answers