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

Scripting troubles.

Discussion in 'Scripting' started by RyanP, Nov 15, 2015.

  1. RyanP

    RyanP

    Joined:
    Nov 14, 2015
    Posts:
    3
    I'm trying to get a 2d character to move in a simple platformer, so i copied over the 2d assets package that unity offers and changed the names of some things to fit my assets but i keep getting this problem,
    "NullReferenceException: Object reference not set to an instance of an object
    UnityStandardAssets._2D.moveWorking.FixedUpdate () (at Assets/script/moveWorking.cs:39)"

    I'm not quite sure what it means by an instance of an object.
    here is my code.
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. namespace UnityStandardAssets._2D
    4. {
    5.     public class moveWorking : MonoBehaviour
    6.     {
    7.         [SerializeField] private float m_MaxSpeed = 10f;                    // The fastest the player can travel in the x axis.
    8.         [SerializeField] private float m_JumpForce = 400f;                  // Amount of force added when the player jumps.
    9.         [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;  // Amount of maxSpeed applied to crouching movement. 1 = 100%
    10.         [SerializeField] private bool m_AirControl = false;                 // Whether or not a player can steer while jumping;
    11.         [SerializeField] private LayerMask m_WhatIsGround;                  // A mask determining what is ground to the character
    12.      
    13.         private Transform m_GroundCheck;    // A position marking where to check if the player is grounded.
    14.         const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
    15.         private bool m_Grounded;            // Whether or not the player is grounded.
    16. //        private Transform m_CeilingCheck;   // A position marking where to check for ceilings
    17.         const float k_CeilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
    18.         private Animator m_Anim;            // Reference to the player's animator component.
    19.         private Rigidbody2D m_Rigidbody2D;
    20.         private bool m_FacingRight = true;  // For determining which way the player is currently facing.
    21.      
    22.         private void Awake()
    23.         {
    24.             // Setting up references.
    25.             m_GroundCheck = transform.Find("GroundCheck");
    26. //            m_CeilingCheck = transform.Find("CeilingCheck");
    27.             m_Anim = GetComponent<Animator>();
    28.             m_Rigidbody2D = GetComponent<Rigidbody2D>();
    29.         }
    30.      
    31.      
    32.         private void FixedUpdate()
    33.         {
    34.             m_Grounded = false;
    35.          
    36.             // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    37.             // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    38.             Collider2D[] theDon = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    39.             for (int i = 0; i < theDon.Length; i++)
    40.             {
    41.                 if (theDon[i].gameObject != gameObject)
    42.                     m_Grounded = true;
    43.             }
    44.             m_Anim.SetBool("Ground", m_Grounded);
    45.          
    46.             // Set the vertical animation
    47.             m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
    48.         }
    49.      
    50.      
    51.         public void Move(float move, bool crouch, bool jump)
    52.         {
    53.             // If crouching, check to see if the character can stand up
    54.         //    if (!crouch && m_Anim.GetBool("Crouch"))
    55.             {
    56.                 // If the character has a ceiling preventing them from standing up, keep them crouching
    57.             //    if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    58.                 {
    59.             //        crouch = true;
    60.                 }
    61.             }
    62.          
    63.             // Set whether or not the character is crouching in the animator
    64. //            m_Anim.SetBool("Crouch", crouch);
    65.          
    66.             //only control the player if grounded or airControl is turned on
    67.             if (m_Grounded || m_AirControl)
    68.             {
    69.                 // Reduce the speed if crouching by the crouchSpeed multiplier
    70.                 move = (crouch ? move*m_CrouchSpeed : move);
    71.              
    72.                 // The Speed animator parameter is set to the absolute value of the horizontal input.
    73.                 m_Anim.SetFloat("Speed", Mathf.Abs(move));
    74.              
    75.                 // Move the character
    76.                 m_Rigidbody2D.velocity = new Vector2(move*m_MaxSpeed, m_Rigidbody2D.velocity.y);
    77.              
    78.                 // If the input is moving the player right and the player is facing left...
    79.                 if (move > 0 && !m_FacingRight)
    80.                 {
    81.                     // ... flip the player.
    82.                     Flip();
    83.                 }
    84.                 // Otherwise if the input is moving the player left and the player is facing right...
    85.                 else if (move < 0 && m_FacingRight)
    86.                 {
    87.                     // ... flip the player.
    88.                     Flip();
    89.                 }
    90.             }
    91.             // If the player should jump...
    92.             if (m_Grounded && jump && m_Anim.GetBool("Ground"))
    93.             {
    94.                 // Add a vertical force to the player.
    95.                 m_Grounded = true;
    96.                 m_Anim.SetBool("Ground", false);
    97.                 m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    98.             }
    99.         }
    100.      
    101.      
    102.         private void Flip()
    103.         {
    104.             // Switch the way the player is labelled as facing.
    105.             m_FacingRight = !m_FacingRight;
    106.          
    107.             // Multiply the player's x local scale by -1.
    108.             Vector3 theScale = transform.localScale;
    109.             theScale.x *= -1;
    110.             transform.localScale = theScale;
    111.         }
    112.     }
    113. }
     
  2. michal_k

    michal_k

    Joined:
    Jul 31, 2014
    Posts:
    33
    it means this line didn't find any colliders in the radius. did you set "m_WhatIsGround" properly?
     
  3. RyanP

    RyanP

    Joined:
    Nov 14, 2015
    Posts:
    3
    from another website i figured out that WhatIsGround is the layer, so i switched that with the layer that the objects he is running on, it is still coming up with the same error, but he is now able to move.