Hi-
Newbie Question:
Why does the following function ‘AssignDefaults’ return an error? I’m obviously missing something stupidly simple! I’ve assigned a non-default value to the example variable myVar just so it’s different to begin with. Other scripts attached to game objects will eventually access the GUI-adjusted variables in this script.
var myVar : float = .025;
function OnGui () {
if (GUI.Button (Rect (25, 25, 125, 20),“Restore All Defaults”)) {
AssignDefaults;
}
myVar = GUI.HorizontalSlider (Rect (25, 50, 125, 20), myVar, 0, 1);
}
function AssignDefaults () {
myVar = .125;
}
This script results in the error: “Expressions in statements must only be executed for their side-effects”
:shock: Huh?!?
What does that mean…what am I doing wrong…and how do I do it right?
Any help will be appreciated.
Thanks so much,
bOB
You want AssignDefaults(); rather than AssignDefaults;.
–Eric
Outstanding.
Thanks, Eric.
I do so hate it when a stupid mistake costs me hours. You have spared me from that~!
bOB
P.S. So what do you interpret that error to mean? :!:
Something like “You can’t do that”.
–Eric
It means “this statement does nothing”.
A ‘side effect’ changes the state of something as opposed to just being a value. For example, if you call a function it might change some other variables as well as returning a value. Assigning a value to a variable is also a side effect. Side effects are important for the kinds of language we use with Unity because they’re the part of the program that does actual work instead of just calculating things.
A function name on its own is just a constant value, and values have no side effects. It’d be like writing “1 + 2;” on a line on its own. I’m sure you understand that part, but I’m spelling it out to clarify the terminology.
The compiler assumes that statements which contain nothing but values and which don’t have any side effects (no function calls or assignment) are a mistake and emits an error accordingly.
It is an oddly worded message, though!
NCarter wrote:
“The compiler assumes that statements which contain nothing but values and which don’t have any side effects (no function calls or assignment) are a mistake and emits an error accordingly.”
Thanks for the clear explanation of that error message NCarter. Makes perfect sense in that light, assuming one notices he’s missing his parens on his function() call. Thanks for interpreting the interpreter for me!
Wouldn’t it be nice if error messages spoke more directly to us though? Instead of somewhat mysteriously telling me:
“Expressions in statements must only be executed for their side-effects”
I would much prefer, “Are you trying to call a function? If so, you’re missing your parentheses…”
“…noob.”
And thanks again, Eric for pointing that omission out.
bOB