Good day, All I want to know is how do I reference another gameObjects collider. In my par of code where it says GetComponent this is where I want to reference the other collider not the gameObjects collider with this script attached.
public class CaterpillarMovement : MonoBehaviour
{
public float speed = 7f;
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "BusStop")
{
StartCoroutine(StopTheBus());
}
IEnumerator StopTheBus()
{
speed = 0f;
GetComponent<Collider>().enabled = false;
yield return new WaitForSeconds(5);
speed = 7f;
}
}
[RequireComponent(typeof(Collider))]
public class CaterpillarMovement : MonoBehaviour
{
public float Speed;
private void Reset()
{
Speed = 7;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("BusStop"))
{
StartCoroutine(StopTheBus(other));
}
}
private IEnumerator StopTheBus(Collider otherCollider)
{
Speed = 0f;
otherCollider.enabled = false;
yield return new WaitForSeconds(5);
Speed = 7f;
}
}
You should read up on general programming, as this is some basic fundamental stuff and not really Unity related.
2 Likes
In case you are not aware, all of the OnTriggerEnter/OnCollisionEnter methods will pass in the collider of the other object involved in the collision. This is why there is always a “Collider other” in the function signature.
When in doubt, check the docs:
Note that if you can also call GetComponent on the “other” parameter if you need to get something other than the Collider component.
2 Likes