I’m facing some problems when try to get the enum value and use it as the index of a list. Really couldn’t think of other reasons why my script doesn’t work so I’m guessing there’s some problems when using enum values.
here’s my code
I have several gameobjects with the same script.
In the script there is a public enum which can be set in the inspector of the gameobject
public myTeam Team;
public enum myTeam
{
Team_A7 = 7, Team_A8 = 8, Team_A9 = 9. //etc, it goes long
}
then I have a List of List of string to store some relationships between teams
private List<List<string>> N = new List<List<string>>();
In the start function, I add a tag which is same as the team name to the gameobject (up to here I am sure it is working), and put in relationships in the List of List
void Start()
{
gameObject.tag = Team.ToString();
//login relations. The first Index correspond to the enum value in myTeam, the first element of that sublist is its own team.
N.Insert(7,new List<string> { "Team_A7", "Team_A8" }); //my intention here is that in sublist7, I store the relations of Team_A7: here N[7][0] is its own team string, N[7][1] is "Team_A8" , meaning that Team_A8 is in relationship with it
N.Insert(8,new List<string> { "Team_A8", "Team_A7", "Team_A9" });
N.Insert(9,new List<string> { "Team_A9", "Team_A8" });
}
ok so now everything is prepared, what I want to do in the update function is, check if the gameobject encounters someone that has relation with itself or is in the same team with itself
void Update()
{
Rigidbody rigidBody = gameObject.GetComponent(typeof(Rigidbody)) as Rigidbody;
Vector3 position = rigidBody.position;
Collider[] sight = Physics.OverlapSphere(position, 0.01f);
float far = 0.00000f;
GameObject furtherest = null;
//check if they have relaions, and find the futtherest one
foreach (Collider unit in sight)
{
if (N[(int)Team].Contains(unit.gameObject.tag))
{
Vector3 diff = unit.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance > far)
{
furtherest = unit.gameObject;
far = curDistance;
}
}
//foreach (string matetag in N[(int)Team])
//{
// if (unit.gameObject.CompareTag(matetag))
// {
// Vector3 diff = unit.transform.position - position;
// float curDistance = diff.sqrMagnitude;
// if (curDistance > far)
// {
// furtherest = unit.gameObject;
// far = curDistance;
// }
// }
//}
}
//then do something to the with the furtherest one
}
Sorry for the bad formatting. I’ve tried use list.contain, or use foreach loop to comparetag, also I’ve tried to directly compare the enum string of the other object to to the list component but none of those method works. So the only problem I can guess is that theres something wrong when building the N list or there is something wrong referring to it with N[(int)Team]. Can someone see where’s my problem?