- Home /
Trying to be more efficient. Advice please...
Hi,
I have a script which sets up values for different Enemy Craft and passes the variables onto a spawned prefab, etc. An example of the variable structure is a bit like this:
Enemy 1-----|Health
|Turn Speed
|Move Speed
|Fly Mode
//Not actual code but a rough example of initial Var setup.
//The actually script is MUCH bigger, but this is an accurate representation of the structure at the moment.
var enemy1Prefab : Transform;
var enemy1Health : int;
var enemy1TurnSpeed : float;
var enemy1MoveSpeed : float;
var enemy1FlyMode : boolean;
Everything works great. However, each time I want to add a new enemy, I have to copy/paste/modify chunks of code. Currently I am at 5 enemy and the script is getting huge. This got me thinking that there must be a more efficient way to do this and save time / reduce the amount of errors created with lots of editing. I am thinking an array is probably the best way to go...
I have recently learnt about List arrays, but I am not sure if they are the right way to go. Here are some specific questions:
1 - My goal is to be able to say in the Inspector that I want to create 10 enemies, when I do that 10 slots are created where I can enter the variable values for health etc. Is this the right direction to go?
2 - If an array is the way to go, am I looking at what is called a multidimensional array? Any advice on best practices for this type of thing?
3 - I am even considering about trying my hand at creating a custom editor (http://docs.unity3d.com/Documentation/ScriptReference/Editor.html). Is this overkill for my needs?
Hopefully someone can put me on the right path to being more efficient!
Thanks
Paul
Answer by Seth-Bergman · Oct 18, 2012 at 10:16 AM
What you can do is to create a custom struct or class which contains all of the data members you need.. Then, you can create ten instances of your struct, and each one can be tweaked individually. Here's how it would look:
class enemy
{
var myPrefab : Transform;
var myHealth : int;
var myTurnSpeed : float;
var myMoveSpeed : float;
var myFlyMode : boolean;
// ... etc
}
var enemy1 = new enemy();
var enemy2 = new enemy();
// ... etc
each instance you create (enemy1, enemy2, etc) will have its own complete set of the vars... Then, when you need to add a new enemy, just add another instance..
Hi Seth, Thanks for the suggestion! I've not used Class before but this looks like a perfect time to give it a go. I'll have a play this evening, and will reply / vote after :)
I'm still open to other suggestions / techniques before I give Class a go, so anyone else feel free to chip in.
Seth that was exactly what I looking for! Spent a few days on it and I love what classes can do. It saves so much time. Thanks for the help.
Paul
Your answer
Follow this Question
Related Questions
How do I make a public array of arrays appear in the inspector? 1 Answer
Multidimentional Array for Inventory style system in inspector. 1 Answer
Arrays and the inspector 1 Answer
Array not updating in inspector 0 Answers
How to make a custom type's contents modifiable in an array by inspector? 1 Answer