Search Unity

UnassignedReferenceException: Error?

Discussion in 'Scripting' started by Axtell, Nov 10, 2018.

  1. Axtell

    Axtell

    Joined:
    Nov 10, 2018
    Posts:
    2
    Hello, I am pretty new to Unity, though I been coding for a good while now... I mostly have experience with JavaScript, HTML, some C++, and some Lua... JavaScript and C# are pretty similar and it more widely recommended to use C# with unity, so that's what I started to do... While coding a small "test" game to kinda get use to unity I kept running into this error:

    UnassignedReferenceException: The variable missile of PlayerMovement has not been assigned.
    You probably need to assign the missile variable of the PlayerMovement script in the inspector.
    UnityEngine.Transform.get_position ()
    PlayerMovement.FixedUpdate () (at Assets/Scripts/PlayerMovement.cs:21)



    I am not sure why I am having this since I did, in fact, put the variable for missile to be assigned in the inspector, and what I intended the code for works just fine... However, every time I try to move the player since it is using the variable it will flood the console with that error even though in game it works...

    My PlayerMovement Script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour {
    4.     public Rigidbody rb;
    5.  
    6.     public Transform missile;
    7.  
    8.  
    9.  
    10.    
    11.  
    12.     void FixedUpdate () {
    13.        
    14.  
    15.         if(Input.GetKey("w") && missile.position.y < 58)
    16.         {
    17.             rb.constraints = RigidbodyConstraints.None;
    18.             rb.AddForce(0, 40 * Time.deltaTime, 0);
    19.  
    20.         }
    21.         else if (Input.GetKey("s") && missile.position.y > 40)
    22.         {
    23.             rb.constraints = RigidbodyConstraints.None;
    24.             rb.AddForce(0, -40 * Time.deltaTime, 0);
    25.  
    26.         }
    27.         else
    28.         {
    29.             rb = GetComponent<Rigidbody>();
    30.             rb.constraints = RigidbodyConstraints.FreezePositionY;
    31.         }
    32.  
    33.     }
    34. }
    Any help is appreciated... Thanks in advance...
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    3,825
    If you have assignment in the inspector, then just add void Start method, and assign null to missile.
     
    Axtell likes this.
  3. Axtell

    Axtell

    Joined:
    Nov 10, 2018
    Posts:
    2
    omg, I feel stupid... So basically I had two copies of the script in the inspecter... I had one that I assigned the variables and the other which was at the bottom a bit hidden from me was unassigned so it caused the error... Thanks for your help though...
     
    Antypodish likes this.