- Home /
Question by
SwastikBhattacharyya · Apr 04, 2019 at 03:35 PM ·
listloopscriptableobjectloopingbooleans
Loop through a List to check whether a bool is true within a ScriptableObject
I have a character Scriptable Object 'Character' and it has a bool IsMale. I also have a 'Team' Scriptable Object and it has a list of Characters from the Character Scriptable Object Class. Now, I want to create a custom method in the Team Class for looping through this list and check how many characters are male and how many are not.
Character.cs
using UnityEngine;
// Personal Attributes
public string firstName;
public string middleName;
public string lastName;
public string fullName;
public bool isMale;
Team.cs
using UnityEngine;
public List<Character> characters;
// For adding ten players.
public void AddPlayer(Character p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
{
characters.Add(p1);
characters.Add(p2);
characters.Add(p3);
characters.Add(p4);
characters.Add(p5);
characters.Add(p6);
characters.Add(p7);
characters.Add(p8);
characters.Add(p9);
characters.Add(p10);
}
// I want to loop through these ten characters in the list and tell how many are males and how many are not
I want to store this information in two ints, something like this
int males = "no. of characters with isMale=true";
int females = "no. of characters with isMale=false";
Comment
Best Answer
Answer by SwastikBhattacharyya · Apr 04, 2019 at 03:44 PM
Answering my own question
using System.Linq;
public int Males
{
get
{
return characters.Count(c => c.isMale);
}
}
public int Females
{
get
{
return characters.Count(c => !c.isMale);
}
}
I do want to give the credits. derHugo from StackOverflow answered this to me.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Update List Item 0 Answers
How to set Animation to Looping via script? 0 Answers