I am currently trying to create a pong game from scratch and having a few problems with the code.
On line 12, I’m getting UnityException: Tag: Ball is not defined, and on line 16, I’m getting NullReferenceException: Object is not set to an instance of an object.
Here is the code.
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public float speed = 8;
Vector3 targetPos;
GameObject ballObject;
void Start()
{
Ball ball = GameObject.FindGameObjectWithTag ("Ball").GetComponent<Ball>();
}
void Update ()
{
targetPos = Vector3.Lerp (gameObject.transform.position,ballObject.transform.position,Time.deltaTime * speed);
gameObject.transform.position = new Vector3 (20, targetPos.y, 0);
}
}
Can someone please help me?