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 jmgek · Mar 22, 2015 at 10:05 PM · objects

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.

Comment
Add comment · Show 2
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 Topthink · Nov 12, 2016 at 07:56 AM 0
Share

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.

avatar image jmgek Topthink · Nov 12, 2016 at 06:14 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

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

22 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

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


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