- Home /
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
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
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?
If you post a example what you need, its more easy to help you. I guess what you need but not have sure. Sorry.
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.
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.
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.)
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?
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.
Thanks, I think I have some understanding of it now. I really appreciate your help.