lights on and off

Before I posted here I did a search and found tons of solutions to this, but nothing is working and I am getting an error. I added a spot light as a child object to the FPS Controller, the default one provided. I have programming experience in Objective C and C. but here is the FULL code so far.

using UnityEngine;
using System.Collections;

public class lightOnOff : MonoBehaviour {
    var linkedLight : Light; // Light has a red line???
    void Start () {
   
    }

    void Update () {
        if(Input.GetKeyDown("f")){
            linkedLight.enabled = !linkedLight.enabled;
        }

    }
}

On line 5 I am getting a red underline on the word ‘Light’. Why? What am I missing?

Earlier I tried it out myself and use ‘public Gameobject flashlight;’ I then dragged a Spot Light I was using as the flashlight to this script to link it to script. But I was unable to use methods like ‘flashlight.enable’. The way I tried it must be wrong but it seem logical that it should be able to function like that. any help would be nice :slight_smile:

you are mixing unityscript and c# syntax.
In c# you declare variables the following:
Access* Type Name

  • is not required, will default to private

so it schould be something like
private Light linkedLight;

1 Like

Hello, @larswik
Just like @ stated, you’re mixing JS and C# syntax. Please check this link (includes a video) to see a comparison, which will hopefully give you a better look at how the syntax looks for both programming languages!

Thanks guys, I will look into that. But if I make it privet following what @element_wsc said, then I must have to link to that object via code? If I make that a public, then I can drag in the object in question and drop it on to the script area to make a reference to that object? I am guessing there is more then 1 way to do this.

Are there different names for assets? for instance I use GameObject for assets, but are there different names when linking to lights?

Thanks for your help and the links!