Domanda

I want to destroy the gameObject(clone) when my player position x and y is the same with gameObject(clone) position x and y. The gameObject appear in my scene when I trigger something else and it has rigidbody and box collider.

I use this code to my player, but it doesn't work

function Update () {
    if(this.transform.position.x==rigidbody.transform.position.x){
        if(this.transform.position.y==rigidbody.transform.position.y){
            Destroy(rigidbody.gameObject);
        }
    }
}

I used the code and like that but it doesn't work too because it didn't find the variable "other"

function Update () {
    var other:Collider;
    if(this.transform.position.x==other.transform.position.x){
        if(this.transform.position.y==other.transform.position.y){
            Destroy(other.gameObject);
        }
    }
}

Can you help me please?

È stato utile?

Soluzione

If you know what's spawning your clones you could spawn them with different names like "gameObject(clone) 2" and then call Destroy().

Destroy(gameObject.Find("gameObject(clone) 2"));

In general though as long as you can identify one object from the other this shouldn't be a problem.

If you have reference to the object you do not wan't deleted then you could perform a .Equals to avoid deleting it.

// C#
foreach(GameObject go in clones) {
    if(!go.Equals(playerObject) {
        Destroy(go);
    }
}

I'd favor the initial approach though. You should have reference or indenifiers for any object in your scene so you can manage them.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top