Error CS(11,10)an object reference is required non static member 'UnityEngine.Light.intensity‘

Hi, I want to write a script to control the light. When the player walks into a certain area and the light turns on and when they leave the light turns off. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class On : MonoBehaviour {
	public float intensity;
	// turn on the light
	public class ExampleClass : MonoBehaviour {
		void OnTriggerEnter(Collider other) {
			Light.intensity = 4.67F;
		}
	}

	// Turn off the light
	void OnTriggerExit(Collider other) {
		Light.intensity = 0.00F;

	}
}

I just got into coding so I am a little confused.

You need a reference to a specific light component to change the intensity of that component.

You can do that like this:

 public class On : MonoBehaviour {
     public Light lightComponent; // Inspector field

     // turn on the light
     public class ExampleClass : MonoBehaviour {
         void OnTriggerEnter(Collider other) {
             lightComponent.intensity = 4.67F;
         }
     }
 
     // Turn off the light
     void OnTriggerExit(Collider other) {
         lightComponent.intensity = 0.00F;
 
     }
 }

This will create a field in your script component’s Inspector that you can drag a Light component into, and that Light component will be assigned to the lightComponent field when the scene initialized. You can get more details on this subject here and here.

For more help on fundamentals of using C#, the official scripting tutorials have some explanations.

This is a beginners coding problem, essentially what your error is saying is that you don’t actually have a reference to an Object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class On : MonoBehaviour {
     public float intensity;
     public Light light;
     // turn on the light
     public class ExampleClass : MonoBehaviour {
         void OnTriggerEnter(Collider other) {
             light.intensity = 4.67F;
         }
     }
 
     // Turn off the light
     void OnTriggerExit(Collider other) {
         light.intensity = 0.00F;
 
     }
 }

Would be the proper way, and you would need to either drag and drop the light component from the editor, or run a GetComponent() method if it is attached to the object.

Basically to break it down for you, Light is a Type, you can’t reference classes that aren’t static as actual objects, because they are simply Types at that point. So you have to have an instance of the type, and reference the instance itself.

for example

public class Item
{
    public int count;
}

public class itemHandler
{
    public void handleItem()
    {
        Item.count = 5;
    }
}

See I am only referencing the class of type Item, not an instance of it.
So this would be an example of a working version.

    public class Item
    {
        public int count;
    }
    
    public class itemHandler
    {
        public Item item;
        public void handleItem()
        {
           if (item == null)
          {
              item = new Item();
          }
            item .count = 5;
        }
    }

I hope this helps.