Search Unity

Problem with JavaScript

Discussion in 'Scripting' started by Rin-Kageyama, Mar 26, 2015.

  1. Rin-Kageyama

    Rin-Kageyama

    Joined:
    Mar 11, 2015
    Posts:
    9
    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):
    1. #pragma strict
    2.  
    3. var Range : float = 1000;
    4. var Force : float = 1000;
    5. var Clips : int = 3;
    6. var BulletsPerClip : float = 30;
    7. var ReloadTimer : float = 0;
    8. var ReloadCooler : float = 3.3;
    9. var Damage : float = 17;
    10. var BulletsLeft : float = 30;
    11. var ShotTimer : float = 0;
    12. var ShotCooler : float = 0.0;
    13. var HitParticles : ParticleEmitter;
    14.  
    15. 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] () {
    16.  
    17. BulletsLeft = BulletsPerClip;
    18. HitParticles.emit = false;
    19.  
    20. }
    21.  
    22. 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] () {
    23.  
    24. if (ReloadTimer > 0){
    25. ReloadTimer -= Time.deltaTime;
    26. }
    27. if (ReloadTimer < 0){
    28. ReloadTimer = 0;
    29. }
    30. if (ShotTimer > 0){
    31. ShotTimer -= Time.deltaTime;
    32. }
    33. if (ShotTimer < 0){
    34. ShotTimer = 0;
    35. }
    36. if (BulletsLeft < 0){
    37. BulletsLeft = 0;
    38. }
    39. if (Input.GetMouseButtonDown(0) && ShotTimer == 0){
    40. if(BulletsLeft > 0){
    41. ShotTimer = ShotCooler;
    42. Shot();
    43. BulletsLeft -- 1;
    44. }
    45. }
    46. if (BulletsLeft == 0 && Clips == 0){
    47. ShotTimer = ShotCooler;
    48. }
    49. if (BulletsLeft == 0 && Clips > 0 && ReloadTimer == 0){
    50. Reload();
    51. }
    52.  
    53. funtion Shot(){
    54. var Hit : RayCastHit;
    55. var DirectionRay = transform.TransformDirection(Vector3.forward);
    56. Debug.DrawRay(tranform.position, DirectionRay * Range, Color.red);
    57.  
    58. if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)){
    59. if(Hit.rigidbody){
    60. if(HitParticles){
    61. HitParticles.transform.position = Hit.point;
    62. HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
    63. HitParticles.Emit;
    64. Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
    65. Hit.collider.SendMessageUpwards("Apply Damage", Damage, SendMessageOptions.DontRequireReceiver);
    66. }
    67. }
    68. }
    69. }
    70.  
    71. function (Reload){
    72. ReloadTimer = ReloadCooler;
    73. yield WaitForSeconds(ReloadTimer);
    74. BulletsLeft = BulletsPerClip;
    75. Clips --;
    76. };
    77. }
     

    Attached Files:

  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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".
     
    blizzy likes this.
  3. Rin-Kageyama

    Rin-Kageyama

    Joined:
    Mar 11, 2015
    Posts:
    9
    [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.
     
  4. Rin-Kageyama

    Rin-Kageyama

    Joined:
    Mar 11, 2015
    Posts:
    9

    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 :/ *
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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).
     
  6. Rin-Kageyama

    Rin-Kageyama

    Joined:
    Mar 11, 2015
    Posts:
    9

    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?
     
  7. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    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
     
  8. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    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.
     
  9. Rin-Kageyama

    Rin-Kageyama

    Joined:
    Mar 11, 2015
    Posts:
    9
    Thank you very much~! All errors solved.