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

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00101.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 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.
        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
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

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