- Home /
Increasing through a list after an object has been destroyed
Recently, I've been working on an educational FPS game - once you 'tag' an enemy (Destroy(GameObject) etc on a prefab that's spawned on level load), the idea is that a variable will increase through a list of preset sums which is going to be displayed.
The one problem I've come across, is having the list count be increased after the correct enemy (each is named with their answer, e.g. EnemyNumber1, EnemyNumber2 etc). This is the last thing I have yet to code and I'm come across a blank.
Basically, I want to increase through a list after a certain prefab has been destroyed. This would then be put into another variable to track it and used to output to a text line. Does anyone have any advice for this specific thing?
Thanks.
What do you mean by this sentence:
"I want to increase through a list after a certain prefab has been destroyed"
specifically, what do you mean by "increase through a list"?
I have a list of GameObjects, ins$$anonymous$$d of an array (but it basically functions as a 1D array - I'm using C# by the way). In essence, I want to destroy a certain prefab, causing the list to move onto the next string and for this to be displayed.
Thanks.
It's not really clear to me, but if you increase an int every time this happens and then use the respective index from the List? Debug.Log(GOList[index].name);
Answer by stepan-stulov · Aug 17, 2015 at 03:11 PM
From your very blurry description I think what you want is to store a count of destroyed enemies grouped by their names. A'la: I have destroyed 3 Joes and 5 Bens.
Here is what you can do:
public class DestroyedEnemyEntry
{
public string name;
public int count;
}
// Later
private Dictionary<string, DestroyedEnemyEntry> _destroyedEnemyEntriesByName = ...;
// Later
_destroyedEnemyEntriesByName["Joe"] = new DestroyedEnemyEntry("Joe", 0);
_destroyedEnemyEntriesByName["Ben"] = new DestroyedEnemyEntry("Ben", 0);
// Later
string destroyedEnemyName = /* However you communicate named enemy destruction */;
_destroyedEnemyEntriesByName[destrioyedEnemyName].count ++;
Debug.Log(string.Format("There have been {0} of {1}s destroyed", _destroyedEnemyEntriesByName[destrioyedEnemyName].count.ToString(), destrioyedEnemyName));
Answer by Chrisasan · Aug 17, 2015 at 03:11 PM
Check if the gameObject is true, if it is true then you can perform work on it, if it is false, then move on and not do any work on it.
public var arr : GameObject[];
for(var scan=0;scan < arr.length;scan++) { if(arr[scan]) {... work on it}
}
Your answer
Follow this Question
Related Questions
Scripts/buttons getting old (prefab?) values of variables 1 Answer
Prefab Script List not being assigned as unique - can you see why? 1 Answer
Create customizable vehicles ingame with prefabs? 0 Answers
How many variables can i have of type 'Prefab' in a scene at one time? 1 Answer
Set variables from a prefab 0 Answers