- Home /
Duplicate Question
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();
}
}
It's your bad way of scripting. Read the questions about nullreferenceexception.
Follow this Question
Related Questions
weird NullReferenceException 1 Answer
An object reference that seems pretty set-to-an-instance-of-an-object to me 0 Answers
nullreference exeptio, object reference not set to an instance of an object 1 Answer
Missing references and incorrect values 0 Answers
Accessing other script's variable into another script 0 Answers