Code for enabling bool 'isTrigger'

I have a Cube with a Box Collider attached. How do I set isTrigger to true using code?

Here’s what I found on a 2012 forum post… but sadly gives a compiler error “Definition not found for isTrigger”.

using UnityEngine;
using System.Collections;

public class HitTrigger : MonoBehaviour {

    // Use this for initialization
    void Start () {
        collider.isTrigger = true;
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

Among the subtler code changes in Unity 5 was doing away with most of the shortcut component accessors like ‘collider’. They left just ‘transform’ because that one is always present; others require GetComponent calls. So now that code would look like:

void Start() {
    GetComponent<Collider>().isTrigger = true;
}
1 Like

Works… this is also useful info… thank you!