茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671308 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_565.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

Game Play Maker : [轉貼]Turning on a directional light through scripting, later on switch

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Turning on a directional light through scripting, later on switch
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.
        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
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[分享]如何在 Unity 裡面用程式控制燈的開關呢?
上面看到的都不是 C# 語法,這裡提一下冷日的作法好了!
如何在 Unity 裡面用程式控制燈的開關呢?
//先宣告一個你要控制的燈
    private Light mainLight;

    void Awake()        //然後用 GameObject.Find 去找你要設定的那盞燈
    {
        mainLight = GameObject.Find("Directional light").GetComponent<Light>();
    }

    void Start () //再來就是看你燈要開還是關?(enable 起來就開,enable false 就關
    {
        mainLight.enabled = false;
    }


註:如果你要關燈後一片黑暗的話,記得要去修改一下 Unity 裡面的 Ambient Light 喔!(可參閱:http://unity3diy.blogspot.tw/2015/04/unity-5-render-settings-solution.html)
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|