- Home /
Checking a List
I have two scripts. One called "BaseMonster" and another called "MonstersMoves". Both are attached the to prefab for the monster. I'm trying to have "MonstersMoves" read "BaseMonster" and dig into a List inside the "BaseMonster" script. It needs to see if that List already contains a move and if it does NOT, and the "Level" variable that's also in "BaseMonster" is a certain level then add the move to the list. (The moves are scrips themselves. What I'm resulting in...is that it just keeps adding the move to the list non-stop. What am I doing wrong? The C# code below is from the "MonstersMoves" script.
public List<BaseMove> moves;
void Awake(){
moves = gameObject.GetComponent<BaseMonster>().monstersMoves;
}
void Update(){
SetupMoves(gameObject.GetComponent<BaseMonster>().Level);
}
private void SetupMoves(int level){
if(level == 1 && !moves.Contains(new Scratch())){
moves.Add(new Scratch());
}
Answer by DRRosen3 · Nov 04, 2014 at 10:36 PM
Solved it myself. The problem was that I was checking the List for a NEW instance Scratch each time. By adding an instance of Scratch at the beginning as a variable, I solved the problem.
public List<BaseMove> moves;
public int level;
public Scratch scratch = new Scratch();
void Awake(){
moves = gameObject.GetComponent<BaseMonster>().monstersMoves;
level = gameObject.GetComponent<BaseMonster>().Level;
}
void Update(){
SetupMoves(level);
}
private void SetupMoves(int level){
if(level >= 1 && !moves.Contains(scratch)){
moves.Add(scratch);
}
}
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Show Top 10 Of 1 GameType 2 Answers
Find GameObjects with a certain Script [Solved] 1 Answer
print the whole list to screen 1 Answer