Need Help with Scripting in JavaScript...

It’s been maybe a couple of months since I last posted here on the Unity forums.

When I started out, I was going to use C# as the scripting language in my Unity games. I’m currently re-doing the prototype example in the book ‘Unity 3.x Game Development Essentials,’ and I decide to use JavaScript instead. Considering I create web pages of my own, I though learning JavaScript in Unity would be a great way to gain skills for JavaScript when I use that scripting language in my HTML documents.

As I’m new to JavaScript, I’m right now doing some research through some old JavaScript books I got a hold of, some I am doing my homework. I posted my source code for the script Shooter.js. I’m getting compile errors, and it’s over the ‘h’ and ‘v’ variables in the code. As the error messages say, ‘h’ and ‘v’ aren’t defined. Since I’m new to JavaScript, does anyone know how I’d go about defining these variables? I’m unsure how to do it, but I do know that it should probably go at the top of the file above the functions listed in the code. I’d appreciate any help that anyone can give on this. :face_with_spiral_eyes:

#pragma strict

var bullet : Rigidbody;
var power : float = 1500;
var moveSpeed : float = 5;

function Start () {

}

function Update () {
	transform.Translate(h, v, 0);
	var h : float = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
	var v : float = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

}

Regardless of anything else wrong with the code, that particular error is happening because you’re using the variables BEFORE you define them in:

transform.Translate(h, v, 0);

Yes, I do see the mistake now. Considering I’m new to JavaScript, my inexperience shows. Thanks - I’ll make the change, and hoping it will work once I put that one piece of code in the right place.

I changed the code like you suggested Steve. It now compiles and works. Thanks for the help. :wink:

Yay! :slight_smile:

Thanks for the support Steve. Just got done doing the first tutorial in the book. It involves shooting a projectile at a wall of bricks, and I got the game to build and execute perfectly. Now onto the next tutorial - Survival Island! I’ve practiced making the terrain for Survival Island a few months ago, and that turned out to be pretty fun. I enjoy modelling in 3D anyway. I’ve done some programming in the past, and I can’t wait to tackle scripting in JavaScript. Being able to model AND script - it’s great to have several talents to use when working on projects. It’s easier to find jobs if you’re knowledge and talents are generalized instead of being so specialized and specific. At least I have a few things to fall back on when I need too… :wink: