Hello! I am Daniel, and I need a bit of help with my Unity 3D horror game! I am very new to Unity, and I am a noob at coding. I tried coding a simple flashlight, but it kept giving me the same error! Here is what I wrote:
function Update () {
if (Input.GetKeyDown(“e”)) {
if (Light.UnityEngine.Behaviour.enabled == true)
(light.enabled == false);
else
light.enabled = true;
}
}
and on the , “light.enabled == false” line it gave me an error saying this:
C:\Users\D\Documents\New Unity Project 2\Assets\FlashLight.js(32,32): Error BCE0034: Expressions in statements must only be executed for their side-effects. (BCE0034) (Assembly-UnityScript)
I don’t know how to fix this!
My Unity Version is: 4.6.5f1
I have no other errors other than that! Please help, I want to make my first Unity game good!
== is for comparision, = is for assignment.
Also you need proper syntax.
if (Input.GetKeyDown("e")) {
if (Light.UnityEngine.Behaviour.enabled == true)
(light.enabled == false);
else
light.enabled = true;
}
}
Should be more like this (note the useage of "{}" and "()")
if (Input.GetKeyDown("e"))
{
if (light.enabled == true)
{
light.enabled = false;
}
else
{
light.enabled == false
}
}
alternately, since you are just flipping it, you can just set it to its negative value:
if (Input.GetKeyDown("e"))
{
light.enabled = !light.enabled;
}
look closely at what I posted and how it is different from what you have. The curly braces need be around the else and code. it should be
if(condition)
{
// do stuff
}
else
{
// od other stuff
}
Also capitalization is important if your “Light” is a class, “light” is presumably your variable reference. Make sure to only use that in this case.
And also you still have the “=” signs confused.
// if you are assigning a value to something, use "="
// it is called an assignment operator
myBool = false;
myInt = = 5;
// to compare, you need to use "=="
// this is a comparison operator
if (myBool == true) { // do something }
if (myInt == 5) { // do something }
// when comparing, whatever happens in the statement in side the () is boolean evaluated so:
if (myBool == true){}
// is the same as:
if (myBool) {}
//----------------------------------------------------------------------------//
// though a bit more complex, but very useful is the ternary operator which is for quick conditionals
// and assignment in single shot.
// the expression is like this
mVariable = (condition) ? [value if true] : [value if false];
// so an example woulud be something like this:
int myVal = (x > 5) ? x : 5; // in this case mVal will be capped at 5
// using your need it would look something like this:
light.enabled = (light.enabled == true) ? false : true;
// or simply
light.enabled = (light.enabled) ? false : true;
// you can also combine ternary ops into a single statement so something like this
x = (x > 10) ? 10 : (x < 0) ? 0 : x;
// which is effectivly a clamp, make x never higher than 10, or lower than 0, you can use the basic concept to
// wrap values for pages, or sets of things
x = (x > 10) ? 0 : (x < 0) ? 10 : x;
// which loops in either direction, if over 10, it goes back to 0, if less than 0, goes to 10.
Those are a little more advanced ideas, but kind of shows the larger scope. What you are doing is a core concept in coding, conditionals. They are pretty much everything in creating a game (or anything). Get to know and understand them very well, and spelling and syntax is critical.