對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2015/5/14 9:56:06
Turning on a directional light through scripting, later on switch between two directi
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.
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?
try getting rid of the
in the
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: http://www.unity3dstudent.com/2010/07/beginner-b17-tweaking-components-via-script/
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:
You're not assigning MoonLight to be anything at the moment. So when you tell it to be enabled... you're not actually telling anything to be enabled.
Try 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.
And if you want to turn it on and off with the button, use a boolean.
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 :)
原文出處:Turning on a directional light through scripting, later on switch between two directi | Unity Community
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?
try getting rid of the
: light
in the
MoonLight : Light = GameObject.Find("Light").GetComponent(Light);
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: http://www.unity3dstudent.com/2010/07/beginner-b17-tweaking-components-via-script/
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:
var MoonLight : Light;
function OnGUI()
{
if (GUI.Button(Rect(100,70,100,100),"Click"))
{
MoonLightOn();
}
}
function MoonLightOn()
{
GameObject.Find("MoonLight").GetComponent(Light);
MoonLight.enabled = true;
print("let there be night");
}
Click to expand...
You're not assigning MoonLight to be anything at the moment. So when you tell it to be enabled... you're not actually telling anything to be enabled.
Try this...
var MoonLight : Light;
function OnGUI()
{
if (GUI.Button(Rect(100,70,100,100),"Click"))
{
MoonLightOn();
}
}
function MoonLightOn()
{
var MoonLightObject : GameObject = GameObject.Find("MoonLight");
MoonLight = MoonLightObject.GetComponent(Light);
MoonLight.enabled = true;
}
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 :)
原文出處:Turning on a directional light through scripting, later on switch between two directi | Unity Community