C# list find index of GameObject

Hello !

I am making a game with turn-based combat.

For the combat i am pulling between 1-3 monsters and then placing them.
The monsters are GameObjects.

I then put the monsters in a List so i can check their position from left-right. (left being index 0 and right being index 2 if there is 3 monsters ofc)

My only problem is getting the index of the GameObject…

This bit of code is on my CombatMainController.cs

      void createMonster(){

		float numberOfMobs = Random.Range (1, 4);
		print (numberOfMobs);

		if (numberOfMobs > 2) {
			monsterStartpointX = -4f;
		} 
		else if (numberOfMobs > 1) {
			monsterStartpointX = -2f;
		} 
		else {
			monsterStartpointX = 0f;
		}

		for (int i = 0; i < numberOfMobs; i++) {
			randomMonster = Random.Range(0, monsters.Count);
			GameObject monster = Instantiate(monsters[randomMonster], Vector3.up, Quaternion.identity) as GameObject;
			aliveMonsters.Add(monster);
			Vector3 temp = monster.transform.position;
			temp.x = monsterStartpointX;
			temp.y = monsterStartpointY;
			monster.transform.position = temp;
			monsterStartpointX += 4f;
		}
	}

	public void removeMonster(GameObject GO){
		aliveMonsters.Remove(GO);
	}

I then send the information of the GameObject being clicked from the MonsterScript.cs
using this code

	void OnMouseUp(){
		OnClickGUI = !OnClickGUI;
		//Destroy (gameObject);
	}
	void OnGUI(){
		if (OnClickGUI) {
			Vector3 V = Camera.main.WorldToScreenPoint(transform.position);
			
			if (GUI.Button(new Rect(V.x - 50,Screen.height - V.y,100,30),"Attack")){
				takenDamage();
			}
		}
	}
	void takenDamage(){
		print (gameObject.name);
            //combatController is a reference to my combatController object
		combatController.removeMonster (gameObject);
		//Destroy (gameObject);
	}

What i want to do is to make a check before i remove a monster is to check their position. I was thinking of using this

public void removeMonster(GameObject GO){
		if (aliveMonsters.IndexOf (GO) > 1) {
			//Do this
		}
		aliveMonsters.Remove(GO);
	}

This however just returns -1…

Any suggestions on how to fix it or maybe you have a better way of doing this

There isn’t enough code to execute your example, so I modified it to test. aliveMonsters.IndexOf(GO) returns the corresponding monster index after clicking on it and the [Attack] button. Here is the code:

MonsterScript.cs

using UnityEngine;
using System.Collections;

public class MonsterScript : MonoBehaviour {

	public CombatMainController combatController;

	bool OnClickGUI;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnMouseUp(){
		OnClickGUI = !OnClickGUI;
Debug.Log(gameObject);
		//Destroy (gameObject);
	}
	void OnGUI(){
		if (OnClickGUI) {
			Vector3 V = Camera.main.WorldToScreenPoint(transform.position);
			
			if (GUI.Button(new Rect(V.x - 50,Screen.height - V.y,100,30),"Attack")){
				takenDamage();
			}
		}
	}
	void takenDamage(){
		print (gameObject.name);
		//combatController is a reference to my combatController object
		combatController.removeMonster (gameObject);
		//Destroy (gameObject);
	}
}

CombatMainController.cs

using UnityEngine;
using System.Collections.Generic;

public class CombatMainController : MonoBehaviour {
	public GameObject[] monsterPrefabs;

	float monsterStartpointX;
	float monsterStartpointY;
	List<GameObject> monsters = new List<GameObject>();
	List<GameObject> aliveMonsters = new List<GameObject>();
	int randomMonster;

	// Use this for initialization
	void Start () {
		foreach (GameObject monster in monsterPrefabs) {
			monsters.Add(monster);
			monster.GetComponent<MonsterScript>().combatController = this;
		}
		createMonster();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void createMonster(){
		
		float numberOfMobs = Random.Range (1, 4);
		Debug.Log("numberOfMobs = " + numberOfMobs);
		
		if (numberOfMobs > 2) {
			monsterStartpointX = -4f;
		} 
		else if (numberOfMobs > 1) {
			monsterStartpointX = -2f;
		}
		else {
			monsterStartpointX = 0f;
		}
		
		for (int i = 0; i < numberOfMobs; i++) {
			randomMonster = Random.Range(0, monsters.Count);
			GameObject monster = Instantiate(monsters[randomMonster], Vector3.up, Quaternion.identity) as GameObject;
			aliveMonsters.Add(monster);
			Vector3 temp = monster.transform.position;
			temp.x = monsterStartpointX;
			temp.y = monsterStartpointY;
			monster.transform.position = temp;
			monsterStartpointX += 4f;
		}
	}
	
	public void removeMonster(GameObject GO){
		Debug.Log(aliveMonsters.IndexOf(GO));
		aliveMonsters.Remove(GO);
	}
}