On running or PLay mode

I am Facing an Issues whenever I play the Game / in Play Mode SCENE.
Warning ( Heavy Exclamation Mark Symbol // Red colored “!” :
NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.ScreenTouch () (at Assets/NewScriptforAsteroid/PlayerMovement.cs:35)
PlayerMovement.Update () (at Assets/NewScriptforAsteroid/PlayerMovement.cs:26)

type or paste code here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
    [SerializeField] private float forceMagnitude;
    [SerializeField] private float maxVelocity;
    [SerializeField] private float rotationSpeed; 

    private Rigidbody rb;
    private Camera mainCamera;

    private Vector3 movementDirection;
    // Start is called before the first frame update
    void Start()
        rb = GetComponent<Rigidbody>();
        mainCamera = Camera.main;

    // Update is called once per frame
    void Update()



    private void ScreenTouch()
        if (Touchscreen.current.primaryTouch.press.isPressed) //to check: currently touching screen
            Vector2 tP = Touchscreen.current.primaryTouch.position.ReadValue(); //read on what location/position of screen


            //convert the position from screen space to World Space
            Vector3 worldPosition = mainCamera.ScreenToWorldPoint(tP);
                                /* take   0,0   subtract -x,-y */
            movementDirection  = transform.position - worldPosition;

            movementDirection.z = 0f;

            movementDirection = Vector3.zero;

    private void FixedUpdate()
            if(movementDirection == Vector3.zero) { return; }

            rb.AddForce(movementDirection * forceMagnitude* Time.deltaTime, ForceMode.Force); 

            rb.velocity =Vector3.ClampMagnitude(rb.velocity, maxVelocity);

    void KeepPlayerOnScreen()
                             //Starting with default position
        Vector3 newPosition = transform.position;
        //convert from world to ViewPort Space
        Vector3 viewportPosition_vpP = mainCamera.WorldToViewportPoint(transform.position);

        //llleft & right Screen
        if (viewportPosition_vpP.x > 1)
            newPosition.x = -newPosition.x + 0.1f;  
        else if (viewportPosition_vpP.x < 0)
            newPosition.x = -newPosition.x - 0.1f;

        // top & bottom of the Screen
        if (viewportPosition_vpP.y > 1)
            newPosition.y = -newPosition.y + 0.1f;
        else if (viewportPosition_vpP.y < 0)
            newPosition.y = -newPosition.y - 0.1f;

        transform.position = newPosition;


    void RotateNoseofPlane()
        if (rb.velocity == Vector3.zero) { return; }

       Quaternion targetRotation = Quaternion.LookRotation(rb.velocity, Vector3.back);

        transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed* Time.deltaTime);


Can you point out the specific line in ScreenTouch() that is landing the null reference error?
Unfortunately, it’s not always apparently obvious from counting lines.

for 35

 if (Touchscreen.current.primaryTouch.press.isPressed) //to check: currently touching screen

for 26

  ScreenTouch();  //which is used as method inside  *Update*

Have you set up Mouse Simulates Screen Touches inside the Input Debugger?

Go to Window | Analysis | Input Debugger, then click on Options within the InputDebugger and select Simulate Touch Input From Mouse Or Pen

1 Like

thank you, it solved.
I don’t know the reason why it was showing me error, but yes every after playing on screen game I was forced to “set up Mouse Simulates Screen Touches inside the Input Debugger” repeatedly to solve it ,. I was still unable to get why the “Input Debugger” selection gets RESET. After sometime I faced “unity red exclamation mark” on Screen .
I sent Report, & later on when I re-opened the Project it worked fine as well as the “Input Debugger” which was Resetting itself automatically now has stopped . :slight_smile:

That is something that is normally set once and forget. Sometimes, though, closing and restarting can settle any bugs that may be lurking in Unity.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms