What does "Other" mean ?

Hello,

I hope this question is not a bad one.

However when I read the Scripting source and I see Scripting examples like:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void OnTriggerEnter(Collider other) {
        Destroy(other.gameObject);
    }
}

What does that mean?
What does “other represent”

Thanks

In this case, if you look at the method declaration, they have set the variable name of the Collider object to other. It could have been set to anything the programmer desired, so in this case other refers to the Collider, but other in and of itself means nothing.

In this example, it represents the other collider (the collider that entered this GameObject’s trigger)

1 Like

So when I see the term “other”
would I be replacing that with the name of my Game Object?

So when I see any function like:

void OnTriggerEnter(Collider other)

In essence is “other” a identifying word for me to replace with my Object?

is (Collider playerName) should I be placing in the player name?

and then in the:

Destroy(other.gameObject);

Should I be placing in the object I would want to destroy?

Sorry if I sound confused, I am just trying to understand Functions and how to approach them in Unity.

1 Like

You do not have to replace the word other in that example, it is only an identifying variable name that is used within that method that points to the Collider object. So yes if you wanted you could replace the word other with playerName or anything else you care to call it. The name only matters as a way for you to easily identify in the object within the method that you are dealing with.

(Collider other)

a Collider object we’re going to refer to by using the variable named “other”…

You could call it “x” (doesn’t tell you much about what it is, descriptive names are better), “theThingThatWasCollidedWith” (a little long, but fully descriptive), or even “dave” (:p). If you want to do something to the thing you’ve collided with, you have to use the same “name” as what you’ve stated in the function parameters, so:

void OnTriggerEnter(Collider dave)
{
Debug.Log("What have you got against dave?!");
Destroy(dave.gameObject); // destroy the gameObject attached to the Collider referenced by the variable named dave
}
2 Likes

Thanks! sorry so late with my response.

That explanation helped quit a bit.