Issue with globals.

I am learning Unity using JavaScript. I’m not a noob to scripting in general, but am at the beginning of this particular learning curve. I did several searches, but found nothing relevant (although I did get an error page saying the the search engine was taking a break at some point).

Here is the background…

I have an empty object that casts a ray. This empty object stores where the ray hits as global Vector3 variable.

I am intending to use another script attached to the main camera to use that global Vector3 to place a retical graphic in 2d space via WorldToScreenPoint.

The issue is that it doesn’t see the global variable. Here is the relevant code for the raycaster…

//targetRetical.js

static var tigerRetical : Vector3 = Vector3.zero;

function Update () {

var hit : RaycastHit;

if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3 (0,0,-1)),hit, 150)) {
	
	var tigerRetical : Vector3 = hit.point;
	}
}

And here is the script that is attempting to read the global…

var target : Vector3 = (targetRetical.tigerRetical);

function Update() {
	print ("vector is " + target);
	}

Basic troubleshooting that I have done so far is…

  1. Printing the value of
    “targetRetical” in the first script.
    This works as expected.
  2. Printing the value of “target” in
    the second script. This does not
    work as expected. It prints the
    vector as “0.0, 0.0, 0.0”.
  3. I have added a particle emitter to
    visually confirm that the raycast
    moves with the object casting the
    ray in script #1. This works as expected.
  4. I have also confirmed that I was
    creating globals and reading them from other scripts correctly by creating a
    global Int in script #1 and printing
    it successfully in script #2.

I just don’t get what I’m missing…

2 Answers

2

I'm not sure if this behaves the same in JS than in C#, but in C# Vector3 is a struct, not a class, so you don't get a reference to the Vector3, but a copy of the Vector. This is never changed so it will always be zero.

Since Hellium converted his comment into an answer, let me accept it on your behalf. Though, I'm curious how that actually happened? The playmode tint color certainly doesn't change itself. So at some point in time you had to change it manually to pitch black. -The tint color has immediate effect. Didn't you test run right after the change? I always pick a rather random "redish" color every time I setup Unity but I immediately test run to see if the contrast is good enough to work with and that the color is not too distractive.

The problem is in the if below: the keyword var created a new temporary variable tigerRetical and stored the hit.point in it - the original static tigerRetical never saw a clue of the hit.point. Remove the variable declaration like below and everything will work:

if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3 (0,0,-1)),hit, 150)) {
    tigerRetical = hit.point; // store hit.point in the real tigerRetical!
}

EDITED: Ok, I didn’t see it: you’re assigning tigerRetical to target outside any function, so it will be executed only once when the script is instantiated. You must place this assignment inside Update to have it updated (intended pun):

function Update() {
    var target: Vector3 = targetRetical.tigerRetical; // you don't need the parenthesis
    print ("vector is " + target);
}

If you declare target outside the function, it will have script scope - the variable will be known by everybody in the script, and will retain its contents while the script is alive. Declaring it inside Update, like above, it will have local scope, which means it will exist and be known only inside the function.

The problem is the variable being only initialized to targetRetical.tigerRetical. Take a look at my answer - it was edited to explain this problem.