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;
}
}
}
}
}