Can't add game object to script
I am brand new to C#. I am currently doing the tutorial at: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/coding-for-the-absolute-beginner . For some reason I cannot drag the directional light to the script, nor add it manually by clicking the dot next to "My Light." Any help is appreciated. Here is the current script that I am using:
using UnityEngine;
using System.Collections;
public class Light : MonoBehaviour {
public Light myLight;
void Update () {
if (Input.GetKeyDown ("space")) {
myLight.enabled = !myLight.enabled;
}
}
}
I can't get the quotes to work right.... Sorry
Worked like a charm. $$anonymous$$uch appreciated.
Answer by Statement · Oct 24, 2015 at 09:57 PM
Well the problem is that you have called your script the same name as the Light type.
I'd recommend you rename your script.
using UnityEngine;
using System.Collections;
// Rename script, problem solved. Rename your file to LightToggle also.
public class LightToggle : MonoBehaviour {
public Light myLight;
void Update () {
if (Input.GetKeyDown ("space")) {
myLight.enabled = !myLight.enabled;
}
}
}
Or, if you really, really want to go down that dark path, you need to fully qualify the Light type or it will think you want a reference of your script instead of a Unity light.
using UnityEngine;
using System.Collections;
public class Light : MonoBehaviour {
// You can fully qualify Light, BUT,
// I HIGHLY recommend you rename your script instead.
public UnityEngine.Light myLight;
void Update () {
if (Input.GetKeyDown ("space")) {
myLight.enabled = !myLight.enabled;
}
}
}