I'm trying to make a pong clone. I can't rid of the nullreference error

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

public class Computer : Paddle
{
    Rigidbody2D ball;
    BallSpawner ballSpawner;
    private void Awake()
    {
        ball = GameObject.Find("Ball").GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        ComputerPaddleAI();
    }

    void ComputerPaddleAI()
    {
        if (ball.velocity.x > 0.0f)
        {
            Debug.Log("1");
            if (ball.position.y > this.transform.position.y)
            {
                Debug.Log("2");
                this._rigidbody.velocity = Vector2.up * this.Speed;
            }
            else if (ball.position.y < this.transform.position.y)
            {
                Debug.Log("3");
                this._rigidbody.velocity = Vector2.down * this.Speed;
            }
        }
        else
        {
            if (transform.position.y > 0.0f)
            {
                Debug.Log("5");
                this._rigidbody.velocity = Vector2.down * this.Speed;
            }
            else if (transform.position.y < 0.0f)
            {
                Debug.Log("6");
                this._rigidbody.velocity = Vector2.up * this.Speed;
            }
        }
    }
}

What can be the main problem.

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

public class BallMovement : MonoBehaviour
{
    BallSpawner ballSpawner;
    Transform ballTransform;
    Transform ballStartPoint;
    [SerializeField] float ballSpeed = 200f;
    Rigidbody2D ballRigidBody;
    Vector2 ballDirection;
    [SerializeField] Sprite ballSprite;
    SpriteRenderer spriteRenderer;


    void Awake()
    {
        ballTransform = GetComponent<Transform>(); 
        ballRigidBody = GetComponent<Rigidbody2D>();
        ballSpawner = GameObject.Find("BallSpawner").GetComponent<BallSpawner>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    void Start()
    {
        SetStartMovement();
    }

    void FixedUpdate()
    {
        ballRigidBody.velocity = ballDirection * ballSpeed;
    }
    void SetStartMovement()
    {    
        float x = Random.value < 0.5f ? -1.0f : 1.0f;
        float y = Random.value < 0.5f ? Random.Range(-1.0f, -0.5f):
                                    Random.Range(0.5f, 1.0f);

        ballDirection = new Vector2(x, y);
        ballRigidBody.velocity = ballDirection * ballSpeed;
        //ballRigidBody.AddForce(ballDirection * ballSpeed);

    }


    void OnCollisionEnter2D(Collision2D other)
    {
        if(other.gameObject.tag == "Player")
        {
            ballDirection.x = -ballDirection.x;
        }
        else if(other.gameObject.tag == "Wall")
        {
            ballDirection.y = -ballDirection.y;
        }
        if(other.gameObject.tag == "EndWall")
        {
            ResetBall();
        }
    }

    void ResetBall()
    {
        ballSpawner.isBallAlive = false;
        this.spriteRenderer.enabled = false;
        StartCoroutine(SetNewBall());
    }

    void SetStartPoint()
    {
        this.gameObject.transform.position = ballSpawner.transform.position;
    }
    IEnumerator SetNewBall()
    {
        yield return new WaitForSeconds(1.5f);
        ballSpawner.isBallAlive = true;
        SetStartPoint();
        this.spriteRenderer.enabled = true;
        SetStartMovement();
    }
}

Null reference error was showing somewhere not related. That’s why I was seeking for help. Solution was extracting the ball reference line as a method and call it on awake. Sharing the solved codes:

public class Computer : Paddle
{
    Rigidbody2D ball;
    BallSpawner ballSpawner;
    private void Awake()
    {      
        SetBall();
        myrb = GetComponent<Rigidbody2D>();
        //  ballSpawner = GameObject.Find("BallSpawner").GetComponent<BallSpawner>();
    }

    void FixedUpdate()
    {
        ComputerPaddleAI();
    }

    private void SetBall()
    {
        ball = GameObject.Find("Ball").GetComponent<Rigidbody2D>();
    }

    void ComputerPaddleAI()
    {
        if (ball != null)
        {
            if (ball.velocity.x > 0.0f)
            {
                Debug.Log("1");
                if (ball.position.y > this.transform.position.y)
                {
                    Debug.Log("2");
                    this._rigidbody.velocity = Vector2.up * this.Speed * Time.fixedDeltaTime;
                }
                else if (ball.position.y < this.transform.position.y)
                {
                    Debug.Log("3");
                    this._rigidbody.velocity = Vector2.down * this.Speed * Time.fixedDeltaTime;
                }
            }
            else
            {
                if (transform.position.y > 1.1f)
                {
                    Debug.Log("5");
                    this._rigidbody.velocity = Vector2.down * this.Speed * Time.fixedDeltaTime;
                }
                else if (transform.position.y < -1.1f)
                {
                    Debug.Log("6");
                    this._rigidbody.velocity = Vector2.up * this.Speed * Time.fixedDeltaTime;
                }
            }
        }
    }
}

1 Like

where is the null reference error? The console should help you find the exact line that’s firing it

1 Like

It is on the paddle referencing the ball, i think what happenned is that after scoring, the ball’s spriterenderer being disabled affected the paddle’s ball detection, which made it lose track of it.

ball = GameObject.Find("Ball").GetComponent<Rigidbody2D();

My suggestion was to call it on Awake, but to also put the detection in a method of its own, and then when the ball respawns, calling to it again as the last step of the respawn method.
As far as i know he got it to work, but i’d like @KilluasYoYo to confirm what was the solution, since other users might be interested in it.

Answered on discord #unity channel but forgot to put it in here, my bad since i was really tired yesterday.

1 Like

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

Privacy & Terms