Anyone here good with JavaScript? Need a little help debugging script.

  • Welcome to "The New" Wrestling Smarks Forum!

    I see that you are not currently registered on our forum. It only takes a second, and you can even login with your Facebook! If you would like to register now, pease click here: Register

    Once registered please introduce yourself in our introduction thread which can be found here: Introduction Board


William

The Lunatic Fringe
Joined
Dec 19, 2011
Messages
2,089
Reaction score
78
Points
0
So here's the part of the script I can't seem to fix.

Code:
function Reload () {
    isReloaded = true;
    if(reloadSound){
        audio.PlayOneShot(reloadSound);
        if(currentClip > 0){
            currentAmmo = ammoPerClip;
            currentClip -= 1;
        }
        isReloaded = false;
    }

The errors are:
Assets/Scripts/Shoot2.js(75,10): BCE0044: expecting (, found 'Reload'.
Assets/Scripts/Shoot2.js(75,19): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/Shoot2.js(76,20): BCE0044: expecting :, found '='.

This is a script for a gun that I am using in Unity for my end of school year assignment. I've got everything else sorted out perfectly, no errors at all. Just that little part which I can't seem to figure out and I can't just delete it since it's the reload script and that's a fundamental of a shooter. So if you could give any help in debugging this, please share!

Complete Script:
Code:
#pragma strict
var weaponModel : GameObject;
var shootFrom : Transform;
var playerModel : Transform;
var bulletHole : GameObject;

var shootTimer : float = 0.0f;
var shootCooler : float = 0.0f;

var bulletForce : int = 0;
var bulletDamage : int = 0;
var distanceFired : int = 0;

var timeToReload : flat = 0.0f;

var currentAmmo : int = 0;
var CurrentClip : int = 6;
var ammoPerClip : int = 30;

var isReloaded : boolean = false;
var canShoot : boolean = true;

var reloadSound : AudioClip;
var shootSound : AudioClip;

function Start () {
    currentAmmo = ammPerClip;

}

function Update () {
    if(Input.GetMouseButton(0)){
        if(currentAmmo < 1 && isReloaded == false){
            Reload();
            }
            if(currentAmmo > 0 && shootTimer < 0 && canShoot == true){
                Fire();
            }
    }
   
    shootTime -= Time.deltaTime;
}

function Fire () {
    if(currentAmmo > 0){
        var hit : RaycastHit;
        var direction : Vector3 = transform.TransformDirection(Vector3.forward);
       
        Debug.DrawRay(shootFrom.position, direction * distanceFired, Color.cyan);
       
        if(Physics.Raycast(shootFrom.postion, direction, hit, distanceFired)){
            var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
            if(hit.transform.tag == "Prop"){
                Instantiate(bulletHole, hit.point, hitRoation);
           
            if(hit.rigidbody){
                if(hit.transform.tag == "Enemy"){
                    print("BulletHitEnemy");
                    }
                   
                    hit.rigidbody.AddForceAtPosition(direction * bulletForce, hit.point);
                    }
                }
               
                currentAmmo --;
               
                shootTimer = shootCooler;
               
                if(shootSound){
                    audio.PlayOneShot(shootSound);
                    }
            }
    }
       
function Reload () {
    isReloaded = true;
    if(reloadSound){
        audio.PlayOneShot(reloadSound);
        if(currentClip > 0){
            currentAmmo = ammoPerClip;
            currentClip -= 1;
        }
        isReloaded = false;
    }


Sorry if this is the wrong section! Wasn't really sure where I should post this.
 

William

The Lunatic Fringe
Joined
Dec 19, 2011
Messages
2,089
Reaction score
78
Points
0
I've posted on all the other forums I'm on and still got no reply :c So I thought I'd post here since I know a fair few guys came here from HF.
 

Coon

Resident Rodent
Joined
May 25, 2013
Messages
1,116
Reaction score
1,183
Points
0
Age
33
Oh, it's in Unity, so it's not actually Javascript. Unity just created their own version of ECMAScript and labeled it Javscript because they're Blue.

Not sure I can help without having the Unity interpreter, but I'll give it a look through.

Edit: Also, you should really try C# instead. It gives you so much more control and the structure is way better when using it for gaming logic, because the class structure is a lot more clearer than it is in the "Javascript" support.

Edit 2 (solution probably): You're missing a closing tag ( } ) for the Fire() and Reload() functions, that should fix any of the problems. Pay attention to indentation.
Edit 3 (also solution probably): Actually, theres also a bunch of other missing closing tags around the code.
 

Crayo

The Boss
Joined
Dec 16, 2011
Messages
63,815
Reaction score
6,080
Points
1
Location
United Kingdom of Ambrose
Website
wweforums.net
Oh, it's in Unity, so it's not actually Javascript. Unity just created their own version of ECMAScript and labeled it Javscript because they're Blue.
Not sure I can help without having the Unity interpreter, but I'll give it a look through.

Also, you should really try C# instead. It gives you so much more control and the structure is way better when using it for gaming logic, because the class structure is a lot more clearer than it is in the "Javascript" support.

Edit: ninja'd damn
 

Jonathan

Champion
Joined
Dec 19, 2011
Messages
17,016
Reaction score
2,957
Points
0
Why not just use this:

Code:
function Reload() {
      audio.PlayOneShot(reloadSound); 
      ammo = 30;
}
 

Coon

Resident Rodent
Joined
May 25, 2013
Messages
1,116
Reaction score
1,183
Points
0
Age
33
Why not just use this:

Code:
function Reload() {
      audio.PlayOneShot(reloadSound);
      ammo = 30;
}
That would probably spam the reloading sound if the player presses the key more than once while the game is already running through the reload sequence.
 

Jonathan

Champion
Joined
Dec 19, 2011
Messages
17,016
Reaction score
2,957
Points
0
That would probably spam the reloading sound if the player presses the key more than once while the game is already running through the reload sequence.

Wouldn't his current? Dunno, thought it was easier to re-write his than find a fix for it :lol1:
 

Coon

Resident Rodent
Joined
May 25, 2013
Messages
1,116
Reaction score
1,183
Points
0
Age
33
Wouldn't his current? Dunno, thought it was easier to re-write his than find a fix for it :lol1:
Probably is, considering the amount of messed up opening and closing tags there are. I just tried to go through it and make it look better and fix the indentation, but there's so many missing tags and excess tags at parts the logic isn't clear anymore.

OP: Start again, use c#.
If you're doing it as part of a course, tell your teacher they're wrong.
If you're doing it as part of a tutorial, find a better tutorial.
 
  • Like
Reactions: Jonathan

Jonathan

Champion
Joined
Dec 19, 2011
Messages
17,016
Reaction score
2,957
Points
0
Also;

function Start () {
currentAmmo = ammPerClip;

}

I don't see any ammPerClip declared :jericho:
I do however, see ammoPerClip :umad:
 

Jonathan

Champion
Joined
Dec 19, 2011
Messages
17,016
Reaction score
2,957
Points
0
Like, have you wrote this yourself or written it looking at a tutorial? Either way, one of them sucks.
 

Coon

Resident Rodent
Joined
May 25, 2013
Messages
1,116
Reaction score
1,183
Points
0
Age
33
If you tried writing this from scratch without any guides, don't feel too bad about the errors and typos, shit happens. Just make sure to pay more attention to what you're actually writing and not what you're planning to write. Also, figuring out how to read errors is probably one of the most important things you can do when learning.

Those errors are pretty vague, but it gives you an indication that something messed up around reload(), since it didn't expect it, so you can pretty much guess some code before that is missing because it's interpreting reload() when it shouldn't be.

Edit: But yeah, after you've handed in this project, really consider learning c# instead.

It's dangerous to go alone, take these:
- The official Unity3D forum should be one of your first go-to places. Newbies will be posting their problems so you'll be able to avoid making the same mistakes, and also well-established members will be posting their showcases and sometimes even background-info on how certain projects were done. If you're serious about getting into Unity, these are the people you want to have as your mentors and you should follow in their footsteps, because most of them come from backgrounds developing their own engines and know more about the ins-and-outs of exactly what the Unity3D engine is doing.

- Consider this book. Packt are pretty good, most of the authors I've read under that publication seem to know how to write things simply.

- Read the subreddit for Unity3d every once in a while, you'll keep up to date with problems people are experiencing in real-life projects and any solutions they may have taken to fix them. Sure, some of the material won't help you right now, but glossing over the list and checking out some of the comments will at least leave it tucked away in the back of your mind ready for when you eventually encounter the same problem.

- Browse through the tagged questions for Unity3D on StackOverflow. As with above, most of these will seem confusing at this point, but it's a pretty good indicator to how much you're learning depending on how comfortable you feel reading the questions and seeing how much you understand. Hell, soon you may even be answering them.

- While not strictly related to Unity, following their discontinuation of the magazine, Game Developer Magazine have released an archive of all of their magazines from 1994 to 2013 including any code mentioned in the articles. It's a real nostalgia-trip, and really gives you an insight into how stuff was done back then. Also, seeing an advert for one of the first versions of 3DS Max being released with a minimum hard-drive space requirement of 20MB and costing $2,995 was pretty hilarious. But hey, at least it supports MS DOS 3.3 :D (GDM June, 1994, Last page).
 

William

The Lunatic Fringe
Joined
Dec 19, 2011
Messages
2,089
Reaction score
78
Points
0
Thanks for all the help guys! Especially you coon! :)

I am currently learning C# however though because only me and 1 other kid in the class have any knowledge of C# we weren't allowed to use it as it would be "unfair" to the other students. I wrote this script myself, I haven't really scripted (using JS) since the second or 3rd term this year so I'd definitely be making mistakes at the moment.

I'm thinking I might just try find a decent tutorial on YouTube and follow that considering this isn't working out to well and I don't exactly have a lot of time to try and master a script like this considering it's due next Friday and I've still got to do other scripts for the game.