Hi,
I want to easily switch between two directional lights to visualise day vs night.
I have read posts in here, watched videotuts, and followed the suggestions, but seems my code is somewhat off.
My console spits out “unexpected token” and “insert semicolon”, but cant see where my problem lies.
var MoonLight : Light;
function OnGUI()
{
if (GUI.Button(Rect(100,70,100,100),“Click”))
{
MoonLight : Light = GameObject.Find(“Light”).GetComponent(Light);
Light.enabled = true;
print(“let there be light”);
}
}
I tried various ways of organizing the script, but nevertheless same error or others come up.
To make the actual script+button work, I tried attaching it to a cube for showing the GUI part and getcomponent to find my directional light.
Any pointers?
It doesnt work. But thanks for helping
I copied most of my code from posts in here http://forum.unity3d.com/threads/75510-light-activation-with-script?highlight=turn+light and a tut from Will G here: Unity 3D Student - 3d Modelling
Is it because im trying to attach the functionality to a button, I find that weird?
Will’s script works when enabling light on a cube’s collision, but I want it to enable on a gui.button.
Im ready to start all over with my script, if this approach is off, but cant really understand it.
Update: It seems to work now, dont know why exactly, but for reference I did this:
Actually, it seems like the problem is that the script is overly complicated. You’ve assigned a Moonlight variable to the light component properly. You can then address the light directly. Drilling down with GameObject.Find (“Moonlight”) is unnecessary. It would be easier to just drop the MoonLight game object right into the MoonLight variable in the inspector. The way you’ve written this, you should see that variable with an empty slot sitting there. Just drag your light onto the variable slot. Also, just for accuracy, your variable should probably be “moonLight”. Typically the first letter of a variable is lower case. You’ll then see it show up as Moon Light in the inspector. Unity assumes there’s a word break when it finds a new cap.
var MoonLight : Light;
function OnGUI()
{
if (GUI.Button(Rect(100,70,100,100),"Click"))
{
MoonLight.enabled = true;
}
}
And if you want to turn it on and off with the button, use a boolean.
private var moonOn: boolean = false;
var MoonLight : Light;
function OnGUI()
{
if (GUI.Button(Rect(100,70,100,100),"Click"))
{
moonOn = !moonOn;
MoonLight.enabled = moonOn;
}
}
ohh, yeah I can see what you mean. Thanx for the input
Anyone aware of how to also switch lightmaps made from those directional lights.
Moonlight doesnt really suit my sunlit terrain