質問

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 gameObject){
        print (gameObject);
        aliveMonsters.Remove(gameObject);
    }

print (gameObject) returns: Goblin(Clone) (UnityEngine.GameObject) UnityEngine.MonoBehaviour:print(Object) - which is the GameObject that is being clicked on.

But aliveMonsters.Remove(gameObject); does not remove said GameObject.

This is the code that is attached to the monster

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(){
        combatController.removeMonster (gameObject);
    }

I have no idea what i'm doing wrong and hope you can help me or atleast point me in a better direction.

役に立ちましたか?

解決

On MSDN you can read this about the List.Remove Method:

If type T implements the IEquatable<T> generic interface, the equality comparer is the Equals method of that interface; otherwise, the default equality comparer is Object.Equals.

If neither IEquatable<T> is implemented by the game object nor Object.Equals is overridden then reference equality is used for the comparison and this one will never fail if the given object is really in the list. This is the default behavior of Object.Equals.

I could imagine that either a faulty Object.Equals override or IEquatable<T> implementations exists that makes the Remove method fail.

Another possibility is that you try to remove a clone of the game object. In that case reference equality cannot work! You could fix it by overriding the Object.Equals method and to make it compare object IDs. Or you could remove like this:

int index = aliveMonsters.FindIndex(m => m.ID == monster.ID);
if (index >= 0 ) {
    aliveMonsters.RemoveAt(index);
}

But in any case, make sure that there is really a problem with the list and not just with the game logic. The object might be removed from the list but not from the game.

他のヒント

Could you do something like:

public void removeMonster(GameObject gameObject){
        print (gameObject);
        GameObject objToRemove = allMonsters.DefaultIfEmpty(null)
                                    .FirstOrDefault(f=>f.obj.ID=gameObject.ID);
        if (objToRemove!=null)
            aliveMonsters.Remove(objToRemove);
        else
            throw new Exception("That monster isn't in the list.")
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top