Null reference

I’m getting the same null reference like everyone and i have tried to switch to unity versions but it’s not working please help if you know how to fix this.

Paste in the error message, so we can get a clue where to look. In the error message you should see a list of methods and positions, the first line of references is usually the place to investigage.

I am getting this Null Reference below. Are you getting the same one?

NullReferenceException: Object reference not set to an instance of an object
BallHandler.Update () (at Assets/Scripts/BallHandler.cs:47)

Athough in my case for some reason I put this my code:

I removed the line in the screenshot that says “currentBallRigidbody.isKinematic = false;” and that worked for me.

Judging by the code here, I can only assume that LaunchBall() is setting currentBallRigidbody to null.

Which it should be.

That line should be the first line of LaunchBall(). In LaunchBall, we set the ball back to being controlled by the Physics system and then remove reference to the ball to signal to Update that we don’t want to manipulate it anymore. This means that we have to make the change to the isKinematic before we set the reference to null.

by your code i’m getting null reference in line number 37 when we are checking if we are not touching the screen

Here is the current code I am using. I don’t get any errors with it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class BallHandler : MonoBehaviour
{
    [SerializeField] private GameObject ballPrefab;
    [SerializeField] private Rigidbody2D pivot;
    [SerializeField] private float detachDelay;
    [SerializeField] private float respawnDelay;

    private Camera mainCamera;
    private bool isDragging;
    private Rigidbody2D currentBallRigidbody;
    private SpringJoint2D currentBallSpringJoint;



    // Start is called before the first frame update
    void Start()
    {
        mainCamera = Camera.main;
        SpawnNewBall();
    }

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

        if (currentBallRigidbody == null)
        {
            return;
        }


        if (!Touchscreen.current.primaryTouch.press.isPressed)
        {

            if(isDragging)
            {
                LaunchBall();
            }

            isDragging = false;

            return;
        }

        isDragging = true;

        currentBallRigidbody.isKinematic = true;

        Vector2 touchPosition = Touchscreen.current.primaryTouch.position.ReadValue();

        Vector3 worldPostion = mainCamera.ScreenToWorldPoint(touchPosition);

        currentBallRigidbody.position = worldPostion;

        
    }


    private void SpawnNewBall()
    {
        GameObject ballinstance = Instantiate(ballPrefab, pivot.position, Quaternion.identity);

        currentBallRigidbody = ballinstance.GetComponent<Rigidbody2D>();
        currentBallSpringJoint = ballinstance.GetComponent<SpringJoint2D>();

        currentBallSpringJoint.connectedBody = pivot;
    }

    private void LaunchBall()
    {
        currentBallRigidbody.isKinematic = false;
        currentBallRigidbody = null;

        Invoke(nameof(DetachBall), detachDelay); 
    }


    private void DetachBall()
    {
        currentBallSpringJoint.enabled = false;
        currentBallSpringJoint = null;

        Invoke(nameof(SpawnNewBall), respawnDelay);
    }

}
1 Like

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

Privacy & Terms