Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ninjaboynaru · Jul 05, 2012 at 05:21 PM · lightintensity

I need help with this script. I want to make it so that when you press the space key the light gets brighter then gets dark again. Here is what I have so far.

pragma strict

function Start () {

}

function Update () {

if(Input.GetKeyDown(KeyCode.Space))

light.intensity = 5;

light.range = 20; }

//if you could please explain what you did and why. I have just started learning Unity script.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by AlucardJay · Jul 05, 2012 at 06:30 PM

This is a breakdown of how your script is running.

Update is called every 'frame'.

Check if space has just been pressed (not being held).

intensity = 5 ....

intensity = 20 ....

so Immediately, you have changed the value twice.

You want something that can remember 2 types of 'state' (a condition that can remember on/off)

There is something called a boolean that is a variable type, it can hold a value called true or false.

So now there is something that you can use, and check it's state, like a light switch (but yours is a touch-lamp) =]

Up is ON (true) , down is OFF (false)

Here's how to write a boolean : var myLightSwitch : boolean = false;

Then, to change it in the script : myLightSwitch = false;

So now before changing the switch you have to check "am i on or off (true or false)?"

this can be confusing at first, but is very easy.

normally an if would look like : if (myLightSwitch == true) { do stuff;}

but for true what you will see written is : if (myLightSwitch) { do stuff;} . this is the same as saying myLightSwitch == true

for false what you will see written is : if (!myLightSwitch) { do stuff;} . Note the ! mark this is like saying if( Not myLightSwitch is true )

SO put it all together (make a new script, copy-paste the following code, check the comments in the script) :

 #pragma strict

 // Declare Variables
 public var myLightSwitch : boolean = false; // public, so you can see it in the Inspector
 private var theIntensity : int; // private, so you can Not see it in the Inspector


 function Start() 
 {
     // Assign values to Variables
     theIntensity = 0;
     if (myLightSwitch == true) // check if myLightSwitch was changed to true in the Inspector at start
     {
         theIntensity = 1; // light is on
     }
     light.intensity = theIntensity;
 }


 function Update() 
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         // check if light is On or Off
         if (myLightSwitch) // if (TRUE), light is ON. ** Remember that (!myLightSwitch) means if (FALSE)
         {
             theIntensity = 0; // light off
             light.intensity = theIntensity;
             myLightSwitch = false; // change boolean to now false (off)
         }
         else // did not fill the condition set in the above if statement (no it wasn't true)
         {
             theIntensity = 1; // light on
             light.intensity = theIntensity;
             myLightSwitch = true; // flip the switch !
         }
     }
 }

hope this helps =]

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jul 11, 2012 at 05:18 AM 0
Share

Hellooo? did this help? comments and/or accepting answers on this 'site is appreciated and helps others searching this 'site. Support the 'site that has supported you. I hope my rant on booleans helped in some way =]

avatar image ninjaboynaru · Jul 13, 2012 at 05:19 PM 0
Share

ya this did work but I had to make some changes. Thanks, I have learned some new stuff about scripting. I am better with learning if you give me an example and show me why you did what you did and how. Thanks so much

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Light intensity with mouse scrollwheel 1 Answer

How to change the intensity to go down over time and to go up after colliding with an object. 1 Answer

Changing light colour makes it too bright 3 Answers

GUI Problem 1 Answer

Error CS(11,10)an object reference is required non static member 'UnityEngine.Light.intensity‘ 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges