Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

UnassignedReferenceException: Error?

Discussion in 'Scripting' started by Axtell, Nov 10, 2018 at 11:23 AM.

  1. Axtell

    Axtell

    Joined:
    Saturday
    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:
    1,942
    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:
    Saturday
    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.