Flashlight script help!

Hello everybody, I am a beginner to coding in Unity, and so am trying to start off small with a simple flashlight script, however it is clear I am doing something quite wrong - not only in terms of syntax but also probably logic too. Can anybody help me out? I want to avoid copying other scipts off the internet as there is no learning process involved.

(And no I cannot work out how to format my code on here, I’m new)

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

public class trigger : MonoBehaviour {

public GameObject flashLight;

void Update ()
{
if (Input.GetKeyDown (“f”))
{
gameObject.SetActive = true
}
else
{
gameObject.SetActive = false
}
}
}

Use code tags!

And if you have errors in Unity’s console (and I can tell you do), post them as well! They’re extremely informative, and prevent us from having to read the entire script line by line.

Compile errors: You’re missing semicolons at the end of your SetActive lines. Also, SetActive is a function, so you don’t assign values to it - you pass it as a parameter, like:

gameObject.SetActive(false);

However, later I’m going to recommend you use .enabled, which is a property that you do assign values to - more on that later.

Logic issues: I see 3:

  1. If you set a GameObject to be inactive, scripts won’t run on it - including this one. So you’ll never be able to turn it on once it’s been turned off. Solution here would be to enable/disable the Light component, instead of the whole GameObject.
  2. GetKeyDown is only true for one frame when the button is pressed, so your light would flicker on for an instant then back off. Use GetKey to leave it on while the button is held, or to toggle, use
gameObject.SetActive(!gameObject.activeInHierarchy);
  1. You have a public GameObject reference (“flashLight”) which you’re not using.

Style/good coding practice issues:

  1. Standard practice is for class names to be capitalized, while variable names are lowercase. This will make it much easier for others to read your code, and using a consistent pattern (especially one consistent with Unity’s own classes) will make it easier to remember which is which.
  2. “Trigger” is a really non-descriptive class name. Something more like FlashlightToggle might be appropriate. (Make sure you change the script’s filename to match the class name)
  3. It’s usually better to use a reference to the component you want to reference, rather than the GameObject it’s on.
  4. Use KeyCode.Whatever instead of a string for GetKey functions - less room for mistakes, etc.

So, with all this in mind,

public class FlashlightToggle : MonoBehaviour {
public Light flashLight;
void Update() {
if (Input.GetKeyDown(KeyCode.F) ) {
flashLight.enabled = !flashLight.enabled;
}
}

Woah, thanks so much for the detailed answer! Very helpful :slight_smile: (Unity answers never alerted me that you even replied to this thread) you seem like a very experienced guy, I have just ran into another problem, mind it is quite simple: http://answers.unity3d.com/questions/1346226/trouble-disabling-and-enabling-script-via-another.html but I’m sure you could solve it for me, if you could give me some tips that would be great :slight_smile: