Virtual Joystick issue

Hi there, I am currently developing an android shooting game. I have made two virtual joysticks, one for player movement and the other for directional shooting (and player rotation).

I am getting a NullReferenceException for the following line of code:

playerRigidbody.MovePosition(transform.position + movement);

I have no idea how to fix it. I have a rigidbody component on my player for sure, so I am not sure why this is thrown up. Am I referencing the component incorrectly?

I know the problem is probably very easy to correct but I still consider myself a newbie when it comes to coding, especially in Unity. Can any body please help?

Edit: Here are the player movement script and joystick move script:

Player Move:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

    public float speed = 6f;

    private Vector3 movement;
    private Rigidbody playerRigidbody;

    void Update ()
    {
        float h = JoystickMove.instance.H;
        float v = JoystickMove.instance.V;
        Move (h, v);
        Turning ();
    }

    void Move (float h, float v)
    {
        movement.Set (h, 0f, v);
        movement = movement.normalized * speed * Time.deltaTime;
        playerRigidbody.MovePosition(transform.position + movement);
    }

    void Turning ()
    {
        Vector3 turnDir = new Vector3(JoystickRotate.instance.H , 0f , JoystickRotate.instance.V);

        if (turnDir != Vector3.zero)
        {
            Vector3 playerToMouse = (transform.position + turnDir) - transform.position;
            playerToMouse.y = 0f;
            Quaternion newRotatation = Quaternion.LookRotation(playerToMouse);
            playerRigidbody.MoveRotation(newRotatation);
        }
    }
}

Joystick Script:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class JoystickMove : MonoBehaviour ,IDragHandler,IEndDragHandler {

    public static JoystickMove instance=null;
    public float _speed=6f;
    public float R=90f;

    private float _r;
    private Vector2 centerPos;
    private float _h;
    private float _v;

    public float H
    {
        get{return _h;}
    }

    public float V
    {
        get{return _v;}
    }

    void Awake()
    {
        if(instance!=null)
        {
            Destroy(this.gameObject);
        }else
        {
            instance=this;
        }
    }
     
    void Start()
    {

        _r=1f*Screen.width/960f*R;
        centerPos=GetComponent<RectTransform>().position;
    }

    void SetHAndF(Vector2 pos)
    {         
        Vector2 diff=pos-centerPos;
        float distance=diff.magnitude;

        if(distance>_r)
        {
            pos=centerPos+diff/distance*_r;
        }
         
        GetComponent<RectTransform>().position=pos;
        Vector2 move=pos-centerPos;
        _h=move.x;
        _v=move.y;
    }

    public void OnDrag(PointerEventData data)
    { 
        Vector2 newPos =new Vector2(data.position.x-20f,data.position.y-20f);
        SetHAndF(newPos);
    }

    public void OnEndDrag(PointerEventData data)
    {
        Debug.Log("End Drag"+centerPos);
        GetComponent<RectTransform>().position=centerPos;
        SetHAndF(centerPos);
    }
}

Thanks in advance!

I can see the null reference issue right away.
You created a variable for the Rigidbody, but never assigned it.
If the Rigidbody is on the game object, then in and Awake() method, you can do:

void Awake() {
   playerRigidbody = GetComponent<Rigidbody>();
   }

Now, when you use that variable, you’ll be referencing the component. :slight_smile:

note: if you had been thinking that it was enough to get the type, and give it a name… and because the game object has a rigidbody, it would just pick that one for you… that’s not how it works. :slight_smile:

1 Like

Thank you so much! Silly mistake but I have been trying to figure it out for a while :slight_smile:

Np :slight_smile: