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 Mit00 · Dec 18, 2014 at 03:21 AM · if statementtroubleshootingtorch

Issue with torch script in C#

I'm having difficulty making a spotlight torch in unity. I'm trying to make the torch switch on and off when I press the middle mouse button, but I can't do it. I'm using a bool to determine if it should turn on or off with a bunch of if() statements. Here are two rearrangements of my script that both don't work. If anyone could help me work out the problem I'd be very grateful.

1:

 using UnityEngine;
 using System.Collections;
 
 public class ScrewdriverLight : MonoBehaviour {
 
     //Variable to describe whether or not torch is on.
     public bool Torch = false;
 
 
     void Start () {
         light.intensity = 0;
     }
 
 
     // Update is called once per frame
     void Update () {
         //Torch switches on and off with middle mouse button
         if (Input.GetMouseButtonDown (2)) {
             //If torch is off, switches on and turns Torch bool to true
             if(Torch == false) {
                 light.intensity = 1;
                 Torch = true;
                 Debug.Log ("Torch on");
             }
             //If torch is on, switches off and turns Torch bool to false
             if(Torch == true) {
                 light.intensity = 0;
                 Torch = false;
                 Debug.Log ("Torch off");
             }
         }
     }
 }

2:

 using UnityEngine;
 using System.Collections;
 
 public class ScrewdriverLight : MonoBehaviour {
 
     //Variable to describe whether or not torch is on.
     public bool Torch = false;
 
     // Update is called once per frame
     void Update () {
         //Torch switches on and off with middle mouse button
         if(Torch == false) 
         {
             if (Input.GetMouseButtonDown(2)) 
             {
                 Torch = true;
                 Debug.Log ("Torch on");
             }
         }
         
         if(Torch == true) 
         {
             if (Input.GetMouseButtonDown(2)) 
             {
                 Torch = false;
                 Debug.Log ("Torch off");
             }
         }
         if (Torch == false) {
             light.intensity = 0;        
         }
         if (Torch == true) {
             light.intensity = 1;        
         }
     }
 }

In the future I am planning to place everything under the update function under another if statement determined by a bool to check if the torch is out or if it is away.

I am fairly new to script programming instead of a node based environment, so try not to judge me too harshly.

Comment
Add comment · Show 8
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 zaid87 · Dec 18, 2014 at 03:44 AM 0
Share

At a glance I can't really see anything much... Does it even print out the "Torch off"/ "Torch on" at the moment? And just in case, did you attached this script to an object in the scene?

avatar image Mit00 · Dec 18, 2014 at 04:57 AM 0
Share

It'll print out the "Torch off" but it won't print out the "Torch on" when I press it again.

And it is attached to a spotlight that behaves as the light from the torch.

And I actually just found out that if, after I have entered game mode, I tick the bool Torch box in the script component in the inspector, it will turn on the torch, and then if I press the middle mouse button, it will turn off the torch. From this I think that the problem lies in the part of the script that turns on the light.

avatar image Mit00 · Dec 18, 2014 at 05:29 AM 0
Share

Just another update: the light, in every configuration of the script, the light will successfully turn off, but it will not turn on. Is there some way in which if() statements can only be used one per set of brackets?

avatar image zaid87 · Dec 18, 2014 at 05:37 AM 0
Share

Try use this code ins$$anonymous$$d:

 void Update () {
          //Torch switches on and off with middle mouse button
          if (Input.Get$$anonymous$$ouseButtonDown (2)) {
              //If torch is off, switches on and turns Torch bool to true
              Torch = !Torch;
              if(Torch) {
                  light.intensity = 1;
                  Debug.Log ("Torch on");
              }
              else {
                  light.intensity = 0;
                  Debug.Log ("Torch off");
              }
          }
      }

avatar image zaid87 · Dec 18, 2014 at 05:40 AM 0
Share

$$anonymous$$y guess it was because how you write the IF statements. The Torch value was changed from false to true but then it goes to another IF statement that change it back to false since the value is true.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexander11 · Dec 18, 2014 at 07:46 AM

heres a way of doing it

 public bool torch;
 
 void Update()
 {
     if(Input.GetButtonDown("Fire1")
     {
         torch = !torch; 
     {
 }
 
 

pretty simple :D

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

When pressing F flash does not stay on. 1 Answer

Collision of Character collider not working? 1 Answer

How to play an animation with a trigger? 3 Answers

UI button not working 1 Answer

How to check if an object is rendered? 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