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 IK · Jun 11, 2014 at 12:31 AM · componentnewbieinstancesconstructorclass object

Object Constructors

Greetings,

I am new to unity and I am trying to remake a simple C++ console game I made in unity. However I am having trouble understanding how objects work different between the two. My problems started with trying to setup constructors for a class (city) so I can create multiple instances. At the start of the game I want a random or player chosen amount of objects that have several attributes I'd usually setup via constructor. I have a class (map) handling an array of these objects to assure that the attributes do not overlap (like location) and I am uncertain how to get to actually create the objects once I've generate all the right data that I'd usually use to construct them. How should I do this? I have tried searching for an answer but I think I'm missing a term to get a good search. Are components my answer? I've had some difficult grasping how those could function to fit my dilemma.

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

3 Replies

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

Answer by psyydack · Jun 11, 2014 at 03:25 AM

Here have a simple unity hierarchy http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes

Maybe this can help you manage your classes http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Comment
Add comment · Show 4 · 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 IK · Jun 11, 2014 at 04:37 AM 0
Share

Thanks, I have looked at the video before and it has given me the interpretation I have. It looks like if I want a map class that is responsible for creating map elements I am going to need a lot of internal classes to manage those elements(ex: city, army, fleet). Is there any way I can pullout these classes (while still being able to use constructors) from the map class so my script isn't massive and cluttered?

avatar image psyydack · Jun 11, 2014 at 10:17 PM 0
Share

If you post a example what you need, its more easy to help you. I guess what you need but not have sure. Sorry.

avatar image IK · Jun 12, 2014 at 12:25 AM 0
Share

I have an object that handles the map data. I need it to generate cities and armies and other things and hold them in an array (at least that is what I'd normally do). So at the opening of the game I need it to place cities. To be able to call a constructor to create the cities with the correct starting data (generated by the map game object) the class 'city' would have to be part of the 'map' game object's class, right? I'd end up having as many components as cities (and more as I create armies and what not). So how can I separate the two classes and still have the ability to have the map object class create them? -- Thanks for you help, I think once I can get past this I can really get going. It just doesn't make sense for cities to be components of a map, like a texture or physics element.

avatar image psyydack · Jun 13, 2014 at 04:01 AM 0
Share

I dont know if understand correctly, but ill try. You want to do this (just a quick pseudo code)):

$$anonymous$$ap ( have data, like Cities, etc ) City (have info about civilization, dificult, Army, etc ) Army ( troops, position, etc )


$$anonymous$$ap Constructor var array_cities = [ new City(a), new City(b), n];

City Constructor var army; var dificulty;

Army var troops;

etc.

So, if you want it, just do this :


 using UnityEngine;
 using System.Collections;

 public class $$anonymous$$ap : $$anonymous$$onoBehaviour
 {
     public List<City> arrayCities;
 
     void Awake ()
     {
         arrayCities = new List<City>();
         PopulateCities();
     }
 
     void PopulateCities()
     {
         arrayCities.Add( new City("Brazil", 10);
     }

 }


 using UnityEngine;
 using System.Collections;

 public class City : $$anonymous$$onoBehaviour
 {
     string name;
     int dificult;
     Army army;
 
     void City(string _name, int _dificult)
     {
         name = _name;
         dificult = dificult;
         army = new Army(100);
     }

 }


 using UnityEngine;
 using System.Collections;

 public class Army : $$anonymous$$onoBehaviour
 {
     int troops;
 
     void Army(int _troops)
     {
         troops = _troops;
     }

 }


Hope this help now.

avatar image
0

Answer by Jeff-Kesselman · Jun 11, 2014 at 12:33 AM

Okay,

Forget what you know about C++ object life cycles.

Unity has its own object lifecycle, as do most container type engines.

Please see the Unity tutorials or the book http://www.amazon.com/Development-Essentials-Community-Experience-Distilled/dp/1849691444/ref=sr_1_12?ie=UTF8&qid=1402446747&sr=8-12&keywords=Unity for an introduction to how unity creates, initializes and manages objects (which are called components in the unity world and are opened by a container called a GameObject.)

Comment
Add comment · Show 1 · 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 IK · Jun 11, 2014 at 02:42 AM 0
Share

Thanks, So after looking through several tutorials and the manual (I tried to understand components earlier), I can't really get a conceptual picture of a container type engine. As far as I can figure out, a map would end up being single game object with a massive amount components, which would be defined by classes inside the class of the map game object. This must be a wrong or at least incomplete understanding of this, right?

avatar image
0

Answer by Bunny83 · Jun 12, 2014 at 12:50 AM

The concept of Unity is like this:

  • every (visual / functional) object in the game is a GameObject

  • a GameObject is just a container without any functionality itself besides: hosting components, give the object a name, hold a layer and tag value,

  • Components can be attached to GameObjects to give that object certain aspects like: a visual representation, an audio source, animation, light, ...

  • Every GameObject has a Transform component. This can't be removed since this is actually the "anchor" in the world / scene. It give the object it's position / rotation / scale and serves as parent for other transform components which build up the hierarchy you see in the hierarchy view.

  • Beside built-in components there is also the MonoBehaviour component. It is one of the two baseclasses you can derive your own component from. MonoBehaviour components are ment to add a certain "behaviour" to an object. MonoBehaviours contain your actual game code.

  • In Unity almost all classes are sealed, so you can't derive your own class from them. This is due to the fact that the actual engine is written in native C++ and Mono is used as scripting interface. The only classes which you are allowed to extend are: MonoBehaviour and ScriptableObject. In most cases you will use MonoBehaviour since it's the only one that provides all the callbacks you need. When it comes to editor scripting there are a few more classes, but the engine itself only provides those two.

  • You can use plain .NET / Mono classes for internal / structural use, but most things will be done with MonoBehaviours.

Comment
Add comment · Show 1 · 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 IK · Jun 13, 2014 at 02:56 AM 0
Share

Thanks, I think I have some understanding of it now. I really appreciate your help.

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

24 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 avatar image avatar image

Related Questions

C sharp Initializer not work in boolean member 0 Answers

2D Animation does not start 1 Answer

Is there a way to detect "On Component Attach" and "On Component Remove"?? 2 Answers

Enable Child Component C# 1 Answer

Custom Inspector, Access built-in component icons? 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