Its a main script(called EnemyHub) ive put on an empty gameobject which gets instantiated.
That script should have a public int to decide how long the array should be. then create the array and populate it with enemies. And instantiate the enemies to the enemies is linked to that array. if the array goes empty(all of the enemies has been killed) it destroys the empty EnemyHub script and instantiate a powerup in its place.
The thing is that i dont want to drag and drop prefabs into the array in the inspector as i want the script to handle the population/instantiation/count of enemies left in its list.
I havent decided if i want the same script to be able to handle every kind of enemy im introducing, but if i cant figure out to implement enums so make it easier, ill just copy/past the code and change the name of the gameobject.
As there isnt much talk about Arrays out there cus every solution i see is lists, i felt a need to come here to ask about how to do this.
The thing is that i would like to see what is going on in the inspector, and the solution to see a list in the inspector with wrappers just doesnt fit my needs as i know the number of enemies i want to spawn each time, i would really want to know how to do this with an array. if its impossible, then i have to go for a list.
Ive only worked with Lists on a C# course, and only with strings, not with Unity and instantiation.
List<Foo> myFooList = new List<Foo>();
// TODO: add a bunch of items to myFooList here...
Foo[] myFooArray = myFooList.ToArray();
// turn it back into an array
List<Foo> myOtherFooList = new List<Foo>( myFooArray);
Slamming them back and forth between one collection type and another isn’t something I would recommend doing every single frame, but at Startup for a level or wave, it’s plenty performant.
It sounds like you are asking about procedurally adding enemies to a list at runtime… or an array? Either way, modulo what I said above about their interchangeability, you can use either collection type.
If you would like an example of a game that dynamically produces an ever-increasing number of enemies each wave, adds them to a list to be able to say “okay, all enemies done, next wave” as well as to check all enemies against all bullets, I have exactly such a game you are welcome to look at.
Look at the scene called DemoTwinStickShooter in my proximity_buttons repository.
The enemy script is Enemy1 and the game manager is called TwinStickGameManager.
Game Manager decides how many enemies to make, makes them, then adds them to its own list.
Game Manager also does the collision checking between bullets and enemies.
Enemies maneuver themselves as well as actually ask how close they are to the player to decide if they nabbed him yet, i.e, produce game over.
proximity_buttons is presently hosted at these locations:
Im asking if I can use an array for my enemyHub script. Or if adding GameObjects to it wont work, and i have to make a list instead. but i want to refrain from using a list cus i know the amount the array should hold, and it will not increase its size, as a list can do. but it is possible to (by script) add objects to it, sense the list size if its empty like (if list = null) destroy object) and with that instantiate a powerup?
the procedure would be:
an empty enemymananger spawns an empty gameobject called “enemyHub”.
on enemyHub there is a script that handles what enemies should spawn and what amount. I pre-enter the type and amount by either hardcode or enums.
when the empty gameobject is spawn, it has a timer with a few seconds, or it triggers when its in scene(doesnt matter) and it makes a list by the size its been designated, populates it with enemies which is declared as enemy1 or enemy2 etc… and spawns them. when the enemies are dead = (list =null) its spawns the powerup.
basic shmup 5 enemies following eachother and spawns a powerup when the last one is dead.
i dont want objects prespawned over the map but a script that spawns them when they get to their spawnmarker.
the spawned enemies have their own script on them that makes them move and interact with the player. but i hope the list will keep track if just that gameobject is null.
and another thing. if i spawn more of the same enemyHub, will it be confused by the other spawned enemyHub script? or can they keep eachother apart? like is different spawned enemies from two different enemyHubs gets destroyed, will it nullify the right list?
If the number of things in the collection will never change, then an array is fine. If you want the option to add/remove from the collection after it is created (change the number of things referenced in the collection), then use a List.
It is not clear to me from your description if you will be adding/removing after the collection is initially created. But I typically just use Lists as my default go to regardless, as the performance difference between the two are generally negligible outside of very large collection benchmark testing, and Lists are just more flexible.
Like I said, I can’t tell if you are adding/removing from the collection AFTER the collection is created. As far as deciding between a List and an array. Either solution will leave null references for destroyed GameObjects in the collection. With a List you have the option of simply removing any of them from the List, while with an array you’d have to create a brand new array.
Ive done the array already and it works.
I just have to make the powerup come out from the last destroyed enemy.
Ive done a enemy count function that if the enemy count is equal to the array length it will activate the powerup instantiation from the last living enemy. every enemy has the potential to spawn a powerup, but only the one last alive in the array.
and to answer youre question:
NO, im not adding or removing anything from the array. as i said.
The array is created, populated, and its checked if its null, as in all designated slots made for the gameobjects are empty. if that is null=empty and the bool that 1 enemy checkpoint has been flipped to true and the last enemy is prepared to drop the powerup, the powerup is dropped when the last enemy is destroyed. and so does the empty game object with the enemyHub script on it.
Wait, are you checking if the array is null, or if each object in the array is null? Because those would be two different things.
Quick and dirty example of the difference
public GameObject[] GameObjectArray;
//Returns true if the GameObjectArray is null
bool checkIfArrayIsNull()
{
bool returnValue = false;
if (GameObjectArray == null)
{
returnValue = true;
}
return returnValue;
}
//Returns true if all elements in the GameObjectArray are null
bool checkIfAllArrayElementsAreNull()
{
bool returnValue = true;
foreach (GameObject g in GameObjectArray)
{
if (g != null)
{
returnValue = false;
break;
}
}
return returnValue;
}
Yes i just realized that im checking the wrong value =)
The index of the array will always be the one i set.
I need to check the objects inside the array. havent figured that one out yet. trying to figure it out as we speak. =)
Im trying the bool that returns the array value you wrote. hope it works as intended.
How would i go about checking if there is 1 object left in the array?
like an else if (g == 1) making the g an int somehow?
public GameObject[] GameObjectArray;
//Returns the number of array elements which are not null
int numberOfNonNullInArray()
{
int returnValue = 0;
foreach (GameObject g in GameObjectArray)
{
if (g != null)
{
returnValue++;
}
}
return returnValue;
}
Then you call that method, and check if the return value == 1 and do whatever you want to do
the funny thing is that ive never seen any solution where you make a bool or an int is a function.
and it confuses me a bit. As they are not public and cannot be accessed by another function. like im trying to make it check if its false, if it is, destroy the object. and im trying to make that happend in update as its not costly (what ive heard) to constantly check if a bool is true and false in update.
so both youre bool and int functions confuses me a bit =)
should i make a function containing them where a declared bool is referenced on top?
or should i just throw in those two checkpoints into the update?
or make an eventsystem that subscribes for the event?
if ive know more about the syntax and coding vocabulary i would be asking =) but i do love coding and finding out ways to make things work. i just need to increase my knowledge. and i often do that by “learning by doing”. thats why im just throwing myself into this.
and hoping some good soul will help out if im stuck =)
Can you just share your code where you are trying to do this? Returning a bool or int I thought was pretty common practice. As for not being public, you can just add public to the function declaration and like magic they are public
public int ImAPublicFunctionNow()
{
return 1278392; //meaningless example
}