I am making a JS for shooting a gun. For some reason (not that I'm a noob at all), I got a couple errors. First, it wanted me to add another "}" at the end of my script, though, that would make my "Reload" function, as well as my "Shot" function a part of my "Update" function. I added the bracket, and that error went away. Could someone explain that? Next, at line 76 (I attached the script for you to look at), it wanted me to add a Semicolon (";"), even though nothing is happening there, except the end of my "Reload" function, where I put the closing bracket. I added the Semicolon, and the error went away. Again, can someone explain? The next error, is at line 43. It wants me to add a Semicolon, even though there is already one there. No matter how many I add to it, Unity expects another one. Another, my last error, is that when I began calling my "Reload" function, at line 71, it wanted me to enclose the name of the function in parenthesis, which I do not understand. The code is below, as well as in attachment form, so you can download it. Please help me as soon as possible! Code (JavaScript): #pragma strict var Range : float = 1000; var Force : float = 1000; var Clips : int = 3; var BulletsPerClip : float = 30; var ReloadTimer : float = 0; var ReloadCooler : float = 3.3; var Damage : float = 17; var BulletsLeft : float = 30; var ShotTimer : float = 0; var ShotCooler : float = 0.0; var HitParticles : ParticleEmitter; function [URL='http://forum.unity3d.com/#'][U][B]Start[U][B][IMG]http://cdncache-a.akamaihd.net/items/it/img/arrow-10x10.png[/IMG][/B][/U][/B][/U][/URL] () { BulletsLeft = BulletsPerClip; HitParticles.emit = false; } function [URL='http://forum.unity3d.com/#'][U][B]Update[U][B][IMG]http://cdncache-a.akamaihd.net/items/it/img/arrow-10x10.png[/IMG][/B][/U][/B][/U][/URL] () { if (ReloadTimer > 0){ ReloadTimer -= Time.deltaTime; } if (ReloadTimer < 0){ ReloadTimer = 0; } if (ShotTimer > 0){ ShotTimer -= Time.deltaTime; } if (ShotTimer < 0){ ShotTimer = 0; } if (BulletsLeft < 0){ BulletsLeft = 0; } if (Input.GetMouseButtonDown(0) && ShotTimer == 0){ if(BulletsLeft > 0){ ShotTimer = ShotCooler; Shot(); BulletsLeft -- 1; } } if (BulletsLeft == 0 && Clips == 0){ ShotTimer = ShotCooler; } if (BulletsLeft == 0 && Clips > 0 && ReloadTimer == 0){ Reload(); } funtion Shot(){ var Hit : RayCastHit; var DirectionRay = transform.TransformDirection(Vector3.forward); Debug.DrawRay(tranform.position, DirectionRay * Range, Color.red); if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)){ if(Hit.rigidbody){ if(HitParticles){ HitParticles.transform.position = Hit.point; HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal); HitParticles.Emit; Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point); Hit.collider.SendMessageUpwards("Apply Damage", Damage, SendMessageOptions.DontRequireReceiver); } } } } function (Reload){ ReloadTimer = ReloadCooler; yield WaitForSeconds(ReloadTimer); BulletsLeft = BulletsPerClip; Clips --; }; }
When the compiler tells you that it expected a } on line X, that means that line X was the soonest that it could 100% prove that you needed another } somewhere. But the compiler doesn't magically know what your intent was, so it usually can't be sure where you should have put it. When that happens, you should never blindly add a } on the line it names; you need to actually go through your code manually to figure out where it should go. Problems I'm noticing: On lines 15 and 22, you have a bunch of forum code junk around the function names. I'm assuming that happened somehow when you were posting on the forum and is not in your original. On line 53, you misspelled "function" as "funtion" I'm guessing the missing } should have been inserted on line 52, before you tried to define a function called "Shot".
[QUOTE="Antistone, post: 2040520, member: 561953"]When the compiler tells you that it expected a } on line X, that means that line X was the soonest that it could 100% prove that you needed another } somewhere. But the compiler doesn't magically know what your intent was, so it usually can't be sure where you should have put it. When that happens, you should never blindly add a } on the line it names; you need to actually go through your code manually to figure out where it should go. Problems I'm noticing: On lines 15 and 22, you have a bunch of forum code junk around the function names. I'm assuming that happened somehow when you were posting on the forum and is not in your original. On line 53, you misspelled "function" as "funtion" I'm guessing the missing } should have been inserted on line 52, before you tried to define a function called "Shot". [/QUOTE] Yes, Lines 15 and 22 were an accident, not sure how that happened, but that is not in the original code. It is still telling me I need a Semicolon at line 43. Not sure why. And thank you. You were right about the missing bracket.
Not sure if you're seeing what I'm seeing, but my computer is acting weird with things, like posting the code, and trying to quote. * Edit: Oh. So now the quoting works :/ *
The one that says "BulletsLeft -- 1"? I'm guessing you meant either "BulletsLeft--" or "BulletsLeft -= 1" (which both have the same effect in this case).
Thank you again, though I did figure that out eventually. Now, one more thing, is Unity is giving me an error at line 66 { HitParticles.Emit; } It is telling me that "Expressions in statements must only be executed for their side-effects." Any idea what that means?
Line 76 ... , what does the semicolon do here? This also shows a general problem with your posted code. Please use proper code formatting. This makes it much easier for yourself and others to find errors. For your emitter problem see here: http://docs.unity3d.com/ScriptReference/ParticleEmitter.Emit.html
Emit() is a function, but you're not calling it, because you're missing the (). Instead, you're just evaluating a reference to the Emit function itself, which doesn't have any effect in that particular line of code.