- Home /
Class for spawning units (gameobjects)
Hey guys, I am not sure if someone asked before about this but I didnt find it so...
We are creating a TD game with my classmate and I dont really know how to make several types of units ( with different hp, gold reward, special abilities, ...). I created spawning system with certain amount of units and interval of spawning plus prefab of gameobject in one script and its all working. But the thing is I cant figure it out how to create class of units where will be every attribute that i can later connect to prefabs of every unit and connect it to spawning system as well. I mean like tower shoot and hit one gameobject and the hp of the unit will decrease.
So if you get it please help me. :)
Best regards, MonK
Do u mean?:
public class $$anonymous$$yUnity {
var hp : float;
var gold : float;
var reward : float
var specialAbilites : String;
}
Answer by VesuvianPrime · Feb 13, 2014 at 10:43 PM
Hi MonKcz
If I understand what you're saying, you want to register new units with scripts that are responsible for interacting with them. So for example, a Tower needs to be aware of it's targets even though they may not be immediately related.
This is a tricky subject, and there are a couple ways of solve it. My personal approach so far has been to use a singleton, but singletons are a fairly polarizing subject among programmers so don't be surprised if I get criticised for this post.
Firstly, the wiki singleton implementation: http://wiki.unity3d.com/index.php/Singleton
Using this Singleton MonoBehaviour I would make a Singleton UnitSpawner:
public class UnitSpawner : Singleton<UnitSpawner>
{
}
Now, from here any of your scripts will be able to access the UnitSpawner by using UnitSpawner.Instance.
You could have a property that stores all of the instantiated units so all of the towers have a quick reference to the units.
I would suggest playing with this and seeing what you can do. It's a fairly inelegant solution, but it should get you back on your feet for a bit.
Your answer
Follow this Question
Related Questions
Spawn GameObject Above and Below GameObject 0 Answers
Correct way to spawn and move objects 1 Answer
extra GameObjects Spawning 2 Answers
Check if tower is already placed 1 Answer
GameObject Class understanding 1 Answer