- Home /
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.
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?
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.
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?
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");
}
}
}
$$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.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613174317im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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