- Home /
Make an array with different variables...
I've got a problem with making an array with different variables(for an enemy). I want something like this: (it's visual)
What Position Lives Level type
name x,y,z 200 3 ground
name x,y,z 200 3 ground
name x,y,z 200 3 ground
How can i get this result. (i only know how to make one with one variables)...
Thanks...
Answer by YoungDeveloper · Jan 18, 2014 at 04:53 PM
You could make a class array and input those fields from inspector or hardcode those variables from script. I don't think saving position would be a good idea though, as you will most likely place mobs randomly across the map, so position for each of them would be different.
[System.Serializable]
public class Enemy{
public string thisName;
public Vector3 thisPosition;
public int lives;
public int level;
public string type;
}
public Enemy[] enemyList;
Also, cool down variables for each enemy should be saved on each enemy itself, same as health or else they will share single variable, which will result in crazy house.
But methods for attacking and other activity can be written once, in same script this class array is. So when you can attack you just call the right method, and pass variables if you need such.
Answer by AlucardJay · Jan 18, 2014 at 04:55 PM
note : you havn't specified the programming language you use, all my examples are in uJS
Create your own class :
public class EnemyClass
{
public var name : String;
public var position : Vector3;
public var lives : int;
public var level : int;
public var type : String;
}
Then in your script, declare a class like this :
var enemy1 : EnemyClass;
You can assign values through script :
enemy1.name = "Marauder";
enemy1.position = Vector3( 10, 0, 20 );
enemy1.lives = 200;
enemy1.level = 3;
enemy1.type = "ground";
or simply assign them in the inspector
You can also make an arry of the enemy class :
var enemies : EnemyClass[];
Try it out. Create a new script, attach it to an empty gameObject in a new scene. In the inspector, set the size of the enemies array, then fill in all the information :
public class EnemyClass
{
public var name : String;
public var position : Vector3;
public var lives : int;
public var level : int;
public var type : String;
}
var enemies : EnemyClass[];
function Start()
{
for ( var i : int = 0; i < enemies.Length; i ++ )
{
Debug.Log( "enemies[" + i + "].name = " + enemies[i].name );
Debug.Log( "enemies[" + i + "].position = " + enemies[i].position );
Debug.Log( "enemies[" + i + "].lives = " + enemies[i].lives );
Debug.Log( "enemies[" + i + "].level = " + enemies[i].level );
Debug.Log( "enemies[" + i + "].type = " + enemies[i].type );
}
}
Wow, looks like I was late with this answer! Both Jamora and YoungDeveloper have given great examples.
Also :
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
Here at Unity Answers, Answer means Solution, not Response.
Read this page : http://answers.unity3d.com/page/newuser.html
Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
I have converted this one for you.
But you still had to post the longest and most bad ass answer haha
This is called Object Oriented Program$$anonymous$$g. There are lots of tutorials and information available on the Internet, but since we're on Unity Answers, I feel I should link to the Unity program$$anonymous$$g tutorials here. Beginner lesson 23 is most likely of particular interest to the OP.
lol, sometimes I do try to help people (ins$$anonymous$$d of just telling people to format code, don't post comments as answers, etc etc) =]
Answer by ProjectCryken · Jan 18, 2014 at 04:34 PM
I think 5 different arrays would be suitable, a string, a Vector3, two ints and another string array.
How can you attach all of them??? I want one array made of different arrays. How can i do that???