I’ve found 3 or 4 different examples of using global variables, but they all generate errors for me. And by global variable, I mean a var I can reference in different scripts attached to different objects.
says the variable is “not a member of callable() as void.”
And there were a few others I found that didn’t work either.
So let’s say I have a script called “Main” that has a variable declared as follows:
static var x = 0.0;
Then I have another script called “Move Object” attached to another object.
This code, in the Move Object script, doesn’t compile even though it looks just like the example in the script reference:
Main.x = 1.0;
How do I reference the variable x declared in the Main script from the Move Object script? Do I declare that variable in the Move Object script too? Does the script name have to be in quotes? Is the name of the script its file name, in this case “Main.js”?
AFAIK, global variables are not the best way of programming/scripting.
What I’d do instead is to create a separate class that would hold public variables. And I’d instantiate this class everywhere I need to access and modify those variables.
This actually doesn’t happen normally, but I can’t tell from context what it is that you’re doing differently. All the code examples you gave compile fine and work correctly.
Cool, thanks. Although I just realized you did provide the necessary information before, but I missed it: namely, Main is a reserved class name, so you should use something else. The error message in this case is not really very helpful in pointing out what the actual problem is.
Still, one thing you should probably do is replace
static var gPaperTop;
with either
static var gPaperTop : Vector3;
or something like
static var gPaperTop = Vector3.zero;
Otherwise the variable is dynamically typed, which is much slower. Also, you may prefer to use Mathf.Clamp rather than a series of if statements, which will make the code quite a bit simpler.
Thanks. Renaming the script did the trick. Gotta love those cryptic error codes.
And thanks for the other suggestions. Believe it or not, I’m not new to programming, just to Unity. Since I started using Director about 10 years ago, I’ve done little else. I’ve forgotten what it’s like to use a more standard programming language. Like getting used to arrays starting with zero again. And all the math functions that I’ve had to write myself in Director (like arcsin and arccos). It’s all these little things, like how to use globals and how to interact with the mouse, that are bogging me down.
Indeed, the Unity API is very large, and it takes a while to learn your way around. After a couple years or so, I still occasionally find things I didn’t know about. When I first discovered Unity, I think I spent the better part of a week just playing around with the physics engine, and barely touched scripting at all…
I wish I could use the physics engine, but unfortunately they typically don’t generate data accurate enough for a physics simulation (at least the two I’ve tried). And that’s what this is, a port of a physics sim I did in Director. Luckily I work with a physics person who gives me all the formulas needed for our own custom physics engine. So virtually everything needs to be done with scripts.