How do i fill the same array with different values depending on an if statement?
If (lifeforce == 5)
{
var behaviour = new array ((monster),(ork),(witch),...)
}
...
If (lifeforce == 2)
{
var behaviour = new array ((massage),(spa),(food),...)
}
Instantiate(behaviour*,place,rotation);*
Define it outside the IF statement. I’m not sure if you are trying to append to the array or not but if your just trying to reuse the same var and repopulate the values (which is what I think you want) then this below will be a good method. It uses builtin arrays instead of Javascript arrays (builtin arrays are faster and are type cast which is important for performance).
var behavior : GameObject[]; // Behavior defined as GameObject array
if(lifeforce == 5) {
behavior = new GameObject[something.length]; // replace something.length with the array's intended length
// Define the values of the array
}
if(lifeforce == 2) {
behavior = new GameObject[something.length]; // replace something.length with the array's intended length
// Define the values of the array
}
Instantiate(behavior*,place,rotation);*
If you are trying to append the array and make it longer thats different. This above allows you to use the same var for both sets but not simultaneously. The array will populate when the condition is true. Don’t forget to accept this if it answers your question so that others can search it. if(myVar == icosahdron) { behavior = new Vector3[20]; // Defines array of icosahdron for(var n = 0; n < behavior.length; n++) { behavior[n].x = // Your math to create an Icosahdron X vert behavior[n].y = // Your math to create an Icosahdron Y vert behavior[n].z = // Your math to create an Icosahdron Y vert } // End For } // End IF This update will allow you scan through all the array indexes and populate the vector3 values very quickly and very efficiently. With that array you can do what you like. I’m at chow right now so I didn’t have time to plug in the icosahdron math. It’s on wiki so it shouldn’t be too tough. I’d likely build a simple function to calculate the values of each vector3 and just call the function to populate the variables. Don’t forget to accept an answer if it works for ya.
Thanks Angry Marine! "//define the values" is which unity command? Actually the arrays are all different lengths using vector3, If var == icosahdron, then array is icosahedron sides sides (20x Vector3's) If var == dodecahedron, then array is dodecahedron sides (12x Vector3's) I was actually really enjoying the command var array = new array ((a),(b),(c),.....) the other one seems too long!
Thanks Angry Marine! "//define the values" is which unity command? Actually the arrays are all different lengths using vector3, If var == icosahdron, then array is icosahedron sides sides (20x Vector3's) If var == dodecahedron, then array is dodecahedron sides (12x Vector3's) I was actually really enjoying the command var array = new array ((a),(b),(c),.....) the other one seems too long!
– anon27064157