Javascript Noob

Hi Y’all,

I’ve finally made time to learn Unity and am having some trouble.

Here’s the code (I’m thinking it’s the last function that I’ve written incorrectly, but I dunno):

private var boundsArray = new Array();
private var textArray = new Array();
private var curBounds = 0;
private var eye : GameObject;
private var t = 0;

function Start() {
	eye = GameObject.Find("eye");
	makeBounds();
	makeTexts();
}

function Update() {
	readArea();
}

function makeBounds() {
	var minX = renderer.bounds.min.x;
	var minZ = renderer.bounds.min.z;
	var topY = renderer.bounds.size.y;
	var unitX = renderer.bounds.size.x / 15;
	var unitZ = renderer.bounds.size.z / 10;
	var xCent = unitX / 2 + minX;
	var zCent = unitZ / 2 + minZ;
	var yCent = topY / 2;
	var c = 1;
	for (var i = 0; i < 150; i++) {
		boundsArray.Push(Bounds(Vector3(xCent,yCent,zCent),Vector3(unitX,topY,unitZ)));
		if (c == 15) {
			xCent = unitX / 2 + minX;
			zCent += unitZ;
			c = 1;
		} else {
			xCent += unitX;
			c++;
		}
	}
}

function makeTexts() {
	for (var i = 0; i < 150; i++) {
		textArray.Push("bounds" + i);
	}
}

function readArea() {
	for (i = 0; i < 150; i++) {
		if (curBounds != i) {
			if (boundsArray[i].Contains( eye.transform.position )) {
				//t++;
				//print(t);
				curBounds = i;
				print(textArray[i]);
				break;
			}
		}
	}
}

Here’s what I’m trying to do:
I have a large terrain which I want divided into regions that have individual phrases associated with them. The above script is attached to the terrain mesh. I used bounds stored in an array and test when the FPS controller (eye) enters a particular bound then print the phrase from the same index position in another array (which for now contains just dummy text).

What does work:
You’ll notice I have two lines commented out in the last function. If I uncomment them (and comment out the other print command) it appears that entering the bounds is detected because t increments and is printed faithfully whenever I move from region to region, and only when I move from region to region. It works even if I revisit bounds (see What doesn’t work, below). Also, with the code exactly as above, if I move around the terrain without visiting a bound a second time, then the associated phrases are printed correctly. I walked the perimeter of the terrain once and all was well.

What doesn’t work:
If I re-enter a bound, its associated phrase isn’t printed. If I move through a number of bounds, then revisit any of them, none of them trigger the print command.

Thanks for any help.

Re-reading Bounds in the script reference, I think I’ve used it inappropriately. I’ll try something else.

I am not really following what you are looking to do with “t”.

Regards,

– Clint

When uncommented it lets me know that the FPS Controller has been detected entering a different bounds, because t increments. So let’s say I move the FPS Controller (eye) back and forth between two adjacent bounds. t will increment each time the controller crosses from one to the other. In the above code, this test never fails–t increments–even though re-entering a previously visited bounds. print(textArray), on the otherhand, always fails when the Controller enters a previously entered bounds, but never when entering a bounds for the first time. So t lets me know that entering a bounds, even a previously entered one, is returning true. Understand that when I uncomment the t lines, I comment out the print(textArray_); line, and vice versa. And that print(textArray*); never indicates the test returns true when entering a previously visited bounds.*
Yow! I have no idea if I’m explaining myself clearly.
I’m doing something wrong and don’t know what. Am I not understanding variable scope or something? I’ve Googled Javascript tutorials for clues but so far can’t see what I’m doing wrong unless it’s misusing bounds._

Thank you Eric, the collapse button was the problem. Whew! Couldn’t figure out what I wasn’t understanding. Time to delve further into the docs.

Also for the trigger boxes suggestion. I’m just familiarizing myself with Unity. Enjoying it like everybody else seems to be.

Cool. Post some of your stuff when you have something you want to show off. :slight_smile:

–Eric