2D Enemy AI problems.
Ok, I have 3 problems. I'm trying to make a pong game using this tutorial, https://www.youtube.com/watch?v=LWkQA_3Upz8, but come up with these errors.
UnityException: Tag: Ball is not defined! Line 12.
NullReferenceException: Object reference not set to an instance of an object. Line 16
error CS1525: Unexpected symbol `GameObject'. Line 18.
Here is my code.
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour {
public float speed = 8;
Vector3 targetPos;
GameObject ballObject;
void Start()
{
ballObj = GameObject.FindGameObjectWithTag ("Ball");
}
void Update ()
{
targetPos = Vector3.Lerp (GameObject.transform.position,ballObj.transform.position,Time.deltaTime * speed)
GameObject.transform.position = new Vector3 (20,targetPos.y,0);
}
}
Can somebody please help me? This is for the enemy AI.
Answer by Chris333 · Oct 21, 2015 at 10:00 PM
Hi,
you have some spelling errors: ballObject => ballObj You are missing the semicolon after assigning targetPos. Also you trying to acces the transform on the non static field GameObject.
I fixed the erros. Try the script below:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
public float speed = 8;
Vector3 targetPos;
GameObject ballObj;
void Start()
{
ballObj = GameObject.FindGameObjectWithTag("Ball");
}
void Update()
{
targetPos = Vector3.Lerp(transform.position, ballObj.transform.position, Time.deltaTime * speed);
transform.position = new Vector3 (20,targetPos.y,0);
}
}
Your answer
Follow this Question
Related Questions
i am making a pong game and it works fine till i reset the game 1 Answer
I need help with Deleting and Initiating a gameobject 1 Answer
Raycast direction change problem 0 Answers
Android Controls 1 Answer
not the bounce iam looking for 2 Answers