How to Enable/Disable a Light without using a 'Public variable'?

Been messing with this for sometime this evening, and I can’t seem to get it. The method works fine with myLight public, and I drag it into the Inspector, but I’d like to do it in code. The light is a child of the Checkpoint (which holds this script).

public class Checkpoint : MonoBehaviour 
{
	public Light myLight;                  // drag light into the Inspector

	void Start()
	{
		myLight.enabled = false ;
	}

	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.name == "player")
		{
			myLight.enabled = true;
		}
	}
	void OnTriggerExit(Collider other)
	{
		myLight.enabled = false;
	}

}

Also, Is simply doing it as ‘public Light’ considered weak coding? I mean I’d like to be able to FInd and GetComponent in Start() correctly, regardless…Any thoughts would be appreciated.

I personally don’t consider it “weak coding” to assign variables in the inspector.
But for what you’re asking you should be able access light if one is attached to this GameObject

You should still verify that it isnèt null.

Or you can use RequireComponent to ensure therès a light component.

Nor do I. However, overly loose control of scope is often considered “weak”, and many people new to Unity don’t realise that you don’t need to make things public to make them Inspector visible.

Check out [SerializeField] - mark a private field with that and it’ll show up in the Inspector as long as it’s compatible with Unity’s serialisation system. This way you can keep your Inspector interface and your code interface both nice and neat.

If you need more smarts than that, custom Inspectors are the way to go. These are useful when simply setting the value of a field isn’t enough. For instance, if you have a float “percentage” which is meant to be a value between 0 and 1 and you already have it set up as a property which enforces that, simply marking the backing field as [SerializeField] isn’t enough - it’s accessed directly and thus the rules enforced by the property getter and setter are skipped. But if you make a custom Inspector you can instead access it via the property getter and setter, so your rules are applied and you can guarantee valid values.