Need help with Proximity AI script

I’m trying to make a proximity AI script that will made an AI character chase a ball in a soccer game I am making for a school project. I’ve looked at multiple threads online and have asked for help from teachers and students.

This is the script I have. I have done something to improve it but I am still getting compiler errors.

public class GameObject Ball;
float range;
float speed;
Rigidbody rb;

void Start()
{
rb=GetComponent();
}

void Update()
{
range = Vector3.Distance(Ball.transform.position, transform.position);
if (range < 40)
{
transform.LookAt(Ball.transform.position);
}
if (range < 30 && range > 15)
{
rb.AddForce(Vector3.forawrd25speed);
}
}

These are the compiler errors that are popping up for me. I’ve tried multiple things to try and solve them but no matter what I do they still come up in the console log.

error CS1022: Type or namespace definition, or end-of-file expected
error CS0116: A namespace cannot directly contain members such as fields or methods
error CS1514: { expected
error CS1513: } expected

I am using Unity 2018.4.15f1, any help or tips on how to solve the problem would be very much appreciated.

it does not appear you are posting the full script… also you can post it to show code to make it easier to read look at your options above while typing …

Your class isn’t defined properly.

You did:

public class
    //your code here

It should be

public class ClassName {
    //your code here
}

or if you intend to place this script on a GameObject, the class should inherit from Monobehaviour

public class ClassName : MonoBehaviour {
    //your code here
}

Make sure the ClassName matches the file name. Case is important.

Also you’ll need to correct the spelling of forward on this line.

rb.AddForce(Vector3.forawrd*25*speed);

Please use code tags when you post code, it makes it easier to read :slight_smile:

Thanks for the replies everyone sorry for not using the code tag thing I am a bit noobish