hi guis.
i have something like this
private Gameobject [] car;
void Start()
{
car = new gameobject[10];
for ( i = 0 : i < car.length : i++) {
instantiate // i just instantiate a 10 car to a different place by create an array of new vector3 the specify that on the instantiate.
}
what i want to do, is for example access to the first index object and destroy it into another methods, for example Update.
how i should do it?
maybe its a stupid things but if anyone can help me i
ll appriciate it.
I’d personally prefer to use lists for something like this, as they make instantiating and destroying as many as you like very simple.
GameObject car; //this can be whatever game object you like
int numCars = 10;
List<GameObject> objects= new List<GameObject>();
for( int i = 0; i < numCars; i++){
objects.Add(Instantiate(car, desiredPosition, desiredRotation));
}
then you’ll have alist full of the cars that you made that you can access and remove from in any way you choose.
you could run another for statement. Just depends on how you want to call destroying them.
For example if they all have a script on the prefab called health, with a currentHealth public float.
You could call an event to check health of all cars on a hit, or the specific depending on what you’re doing.
If there’s a fire for example, and they all lose health. if you needed to call to if they explode once hitting <1 health.
foreach(gameObject go in objects)
// <----- script name if you didn’t setup previous… calls at the moment.
health carhealth = go.getComponent();
if(carhealth.currenthealth<1)
{
setActive(false) - destroy - pillage- etc
}