Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help, it's so simple but im new!

Discussion in 'Editor & General Support' started by Laaxxn, Aug 2, 2014.

  1. Laaxxn

    Laaxxn

    Joined:
    Aug 25, 2013
    Posts:
    29
    Hi so i get the error There is already a local variable with the name 'hit'.

    Code:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var TheDamage : int = 50;
    4. var Distance : float;
    5. var MaxDistance : float = 1.5;
    6. var NinjaSword : Transform;
    7. var SoilderHit : Transform;
    8.  
    9. function Update ()
    10. {
    11.     if (Input.GetButtonDown("Fire1"))
    12.    
    13.     NinjaSword.animation.Play("NinjaSwordAni");
    14.     var hit : RaycastHit;
    15.    
    16.     if (Input.GetButtonDown("Fire1"))
    17.    
    18.     SoilderHit.animation.Play("soilderhit");
    19.     var hit : RaycastHit;
    20.    
    21.    
    22.     if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
    23.         {
    24.             Distance = hit.distance;
    25.             if (Distance < MaxDistance)
    26.             {
    27.             hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    28.                     }
    29.                 }
    30.             }
    Getting the error on: 16-19
     
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Means exactly what it says. On line 14 you declare a variable called hit, and you try to declare it again on line 19.
     
  3. Laaxxn

    Laaxxn

    Joined:
    Aug 25, 2013
    Posts:
    29
    So I can't use it twice? How do I make both animations play at the same time sen?
    I'm new, so i'm just trying to learn. :)
     
  4. Laaxxn

    Laaxxn

    Joined:
    Aug 25, 2013
    Posts:
    29
    Oh i removed it and now it says

    UnassignedReferenceException: The variable SoilderHit of MeleeSystem has not been assigned.
    You probably need to assign the SoilderHit variable of the MeleeSystem script in the inspector.
     
  5. Marcurios

    Marcurios

    Joined:
    Nov 19, 2012
    Posts:
    88
    you ask " if (Input.GetButtonDown("Fire1"))" twice, can't you just use the statement once and do all your stuff there ?
    and you can declare the hit var at the beginning, just like the other variables.
    and correct me if i'm wrong, but if you use more then one sentence after you use a "if" statement, don't you need to use a "endif" statement as well ?
     
  6. Laaxxn

    Laaxxn

    Joined:
    Aug 25, 2013
    Posts:
    29
    Oh i'm retarded haha. Thanks dude but I still have one problem..
    The animation state soliderhit could not be played because it couldn't be found!
    Please attach an animation clip with the name 'soliderhit' or call this function only for existing animations.

    I'm not sure how :eek:
     
  7. Marcurios

    Marcurios

    Joined:
    Nov 19, 2012
    Posts:
    88
    in your script it says "soilder", and now you say "solider", but don't you mean "Soldier" ?
    i'm sure you misspelled it somewhere, then you get a null reference..

    i also mad a lot of spelling errors when i started scripting, so it wouldn't surprise me if that was the case.
     
  8. Laaxxn

    Laaxxn

    Joined:
    Aug 25, 2013
    Posts:
    29
    Yeah I misspelled it, didn't notice at first. I changed it and still the same

    "The animation state SoldierHit could not be played because it couldn't be found!
    Please attach an animation clip with the name 'SoldierHit' or call this function only for existing animations."

    How do I fix this?
    Thanks for being supportive :)
     
  9. Laaxxn

    Laaxxn

    Joined:
    Aug 25, 2013
    Posts:
    29
    I'm problary gonna start over because it all got screwed up.
     
  10. Marcurios

    Marcurios

    Joined:
    Nov 19, 2012
    Posts:
    88
    drop the animation into the script (inspector) again, cause the link probably got lost after you renamed it, it usually cleares the slot when you rename your variables..