This one is probably pretty simple…
I need to create a variable type for a script that can hold the Vector3 data from an object’s transform.position. However, I can’t seem to figure out a way to do this that doesn’t result in an error like “You are not allowed to call this function when declaring a variable”. Apparently this doesn’t work:
var myVariable : Vector3;
Nor does this:
var myVariable = Vector3(0,0,0);
Unfortunately, the scripting reference covering Vector3 doesn’t seem to explain how one would do this properly.
Any suggestions?
Both of those are correct…must be some other line causing the problem.
–Eric
Weird… not sure why, but it’s working fine now. Only thing I can think of is maybe unity was still in playback mode at some point while I was trying to fix the variable declaration. Other than that, the rest of the script is untouched…
I was almost starting to think I’d have to resort to more sloppy methods like setting up a new 1x1 array to contain it. 
No need; I have a nice script here which you can use to implement the Vector3 using an array of strings. You just write out each digit of each number in plain, easy-to-understand English. And you can modify that easily for other languages right in the Inspector!
True, it only handles integers. For version 2.0, I’m going to make it handle floating-point, too. But already it can do Vector2s and Vector4s just by changing the vectorString to have two or four elements, and you don’t have to modify the code at all! Well, except for changing myPos in the SetPos() function to use something with a Vector2 or Vector4 instead of transform.position!
var vectorString = ["three.seven", "one.zero.zero", "four.two"];
var numberString = "zero.one.two.three.four.five.six.seven.eight.nine";
var numbersInt = "0.1.2.3.4.5.6.7.8.9";
private var numbers : int[];
private var nString : String[];
function Start () {
// Set up an array derived from the numbersInt string
// Because what if they change the order someday?
var nInt = numbersInt.Split("."[0]);
numbers = new int[nInt.Length];
for (i = 0; i < nInt.Length; i++) {
numbers[i] = parseInt(nInt[i]);
}
// Make nString array by splitting numberString
// It's just easier to read that way instead of making it an array
nString = numberString.Split("."[0]);
SetPos();
}
function SetPos () {
// Set our position by parsing each element of the vectorString
var myPos = transform.position;
for (i = 0; i < vectorString.Length; i++) {
myPos[i] = GetNumbers(vectorString[i]);
}
transform.position = myPos;
}
function GetNumbers (s : String) : int {
var thisNumber = s.Split("."[0]);
var number = 0;
var count = 0;
for (i = thisNumber.Length-1; i >= 0; i--) {
for (j = 0; j < nString.Length; j++) {
if (thisNumber[i] == nString[j]) {
break;
}
}
number += numbers[j] * Mathf.Pow(10, count++);
}
return number;
}
(For the humor-impaired: Yes, this is a joke. No, I have no idea why I just wasted 15 minutes of my life on that…I think I’ve been coding too much today. :roll:)
–Eric