• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 27, 2014 at 08:31 PM by Graham-Dunnett for the following reason:

Duplicate Question

avatar image
0
Question by MrHero · Dec 27, 2014 at 08:28 PM · bugnullreferenceexceptionobject-reference-errorobject-reference-not-set

Bug in Object reference ?

so i have a code which has a public variable of my script type and i've assigned to it a game object through unity..everything works perfectly when i select my empty game object in the hierarchy , but once my selection is on something else i get the object reference error.. the error which says :-

NullReferenceException: Object reference not set to an instance of an object _Movement2D._disCharge (._Player player) (at Assets/Sample Assets/2D/Scripts/_Movement2D.cs:89) _Movement2D.Update () (at Assets/Sample Assets/2D/Scripts/_Movement2D.cs:75)

but what im not sure what happens there .. is it a bug? or it's my bad way of scripting things?

i'll post my code that has the error which is showing the error in unity... it's basic code for movement and such ..

 using UnityEngine;
 using System.Collections;
 
 public class _Movement2D : MonoBehaviour {
 
     private Rigidbody2D _refrenceToRigid2D;
     private Animator _refrenceToAnimator;
     private bool _facingRight = true;
     private bool _canJump;
     public float speed;
     public float jumpSpeed;
     public Transform startPos;
     public Transform endPos;
     public LayerMask groundLayer;
     /// <copied start>
     /// charging
     /// </copied end>
     public bool Charging = false;
     public _Player player;
 
     // Use this for initialization
     void Start () {
     
         _refrenceToRigid2D = this.rigidbody2D;
         _refrenceToAnimator = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
     
         float move = Input.GetAxisRaw("Horizontal");
         _refrenceToAnimator.SetFloat("Speed",Mathf.Abs(move));
         _refrenceToRigid2D.velocity = new Vector2(move*speed , _refrenceToRigid2D.velocity.y);
 
         if (_facingRight == true && move < 0)
         {
 
             _facingRight = false;
             transform.rotation = Quaternion.Euler(transform.rotation.x, 180, transform.rotation.z);
         }
 
         else if (_facingRight == false && move > 0)
         {    
 
             _facingRight = true;
             transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.z);
 
         }
 
 
         RaycastHit2D rayInfo = Physics2D.Linecast(startPos.position, endPos.position, groundLayer.value);
 
         if (rayInfo.collider != null)
         {
             _canJump = true;
             _refrenceToAnimator.SetBool("Jump",false);
         }
 
         if (Input.GetKey(KeyCode.Space)&& _canJump == true )
         {
             _refrenceToAnimator.SetBool("Jump",true);
             _refrenceToRigid2D.velocity = new Vector2(_refrenceToRigid2D.velocity.x,jumpSpeed);
             _canJump = false;
         }
 
         ////////////////////////////////////
         ////////////// Charge //////////////
         ////////////////////////////////////
         if (Input.GetButton("Fire1"))
         {
             _charging(player);
         }
         if (Input.GetButtonUp("Fire1"))
         {
             _disCharge(player);
         }
 
     }
 
     void _charging(_Player player)
     {
 
         player.playerArray[0].fCalculateCharge();
 
     }
 
     void _disCharge(_Player player)
     {
         player.playerArray[0].freleaseCharge();
     }
 
 
 }
 


Comment
Comments Locked · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Dec 27, 2014 at 08:31 PM 0
Share

It's your bad way of scripting. Read the questions about nullreferenceexception.

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Upon clicking: Object reference not set to an instance of an object 0 Answers

Script finds everything but still i get "nullreference, object not set to an instance of an object"??? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Why am I getting NullReferenceException on my .Contains()? 1 Answer

Object Reference Not Set Up As An Instance Of An Object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges