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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems

Discussion in 'Scripting' started by Prodigy_, Jul 31, 2014.

  1. Prodigy_

    Prodigy_

    Joined:
    Jul 31, 2014
    Posts:
    2
    Code (JavaScript):
    1. //IMPORTANT NOTE: This script was updated last in tutorial number 6. If you haven't watched it yet, go do it before messaging me with errors :)
    2.  
    3. #pragma strict
    4.  
    5. var TheDammage : int = 50;
    6. var Distance : float;
    7. var MaxDistance : float = 1.5;
    8. var karambit : Transform;
    9.  
    10. function Update ()
    11. {
    12.     if (Input.GetButtonDown("Fire1"))
    13.     {
    14.         karambit.animation.Play("Basic 1");
    15.  
    16.     }
    17. }
    18.  
    19. function AttackDammage ()
    20. {
    21.         //Attack function
    22.         var hit : RaycastHit;
    23.         if (Physics.Raycast (karambit.transform.position, karambit.transform.TransformDirection(Vector3.forward), hit))
    24.         {
    25.             Distance = hit.distance;
    26.             if (Distance < MaxDistance)
    27.             {
    28.                 hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
    29.             }
    30.         }
    31. }
     

    Attached Files:

    Last edited: Jul 31, 2014
  2. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Have you read the error message? The first one is a big clue to what's going on.
     
  3. Prodigy_

    Prodigy_

    Joined:
    Jul 31, 2014
    Posts:
    2
    Okay I fixed the first error but I still don't understand the other 2 ?
     
  4. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    21
    basically the unassignedreferenceexception is telling you that you are trying to use a variable without it having anything in it.

    So in the code above you have declared karambit as a variable of type transform but have not actually assigned anything to it yet - so when unity tries to use Karambit it has an empty transform - you need to assign it to something.
     
  5. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Check out the screenshot.. the variable should be assigned. That's what puzzled me.

    I suggest taking that object apart and putting it back together, re-assigning things freshly.
     
  6. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    21
    yeah that is a good pickup der_r - definitely a re-assemble and see if the error is still there