Hey, well, I am a major noob in coding, and this is my attempt at a basic flashlight script… I am still wondering what I am doing wrong… The extra variables are to allow it to work with my main Inventory system. Any help would be appreciated!!! :3
- Variable names do not have quotes around them.
- Lines need to end with a semi-colon.
- Your logic needs to be placed inside a function, probably one called Update().
- There is no language feature called toggle
- To disable a light you’d use lname.enable = false; (Where the light is called lname).
- Variables need to have type specified. So, your HadFlashlight is probably a boolean variable.
- The && operator is used to combine boolean values, so && 1 is wrong, it might be treated as && true, however that is unnecessary.
There might be other problems with your code. But this should be enough to get you back on track.
You need to look into a coding book first.
-
A variable assuming you are using javascript has the following context
var variableName = “string value here”;
var booleanName = true; -
(HasFlashlight && 1)
is the same as(HasFlashlight)
because 1 is always true -
toggle enable.Light;
I’m not even sure the intent of this, but I’m assuming you want to set a light object on and offvar light; //Some flashlight GameObject
if (Input.GetKeyDown(KeyCode.E) && (HasFlashlight)) light.enabled = ! light.enabled; //Set the enabled state to the opposite of it’s current value