Hello Guys and Gals,
I am making a racing game. I am trying to create waypoints for the AI to follow. I am using GameObject.Find and transform.LookAt to accomplish this. If I put a string directly into Find() like this GameObject.Find(“wayPoint”); it works great. But when I try to use a variable i get an error.
NullReferenceException: Object reference not set to an instance of an object
AiController.go () (at Assets/myAssets/scripts/AiController.cs:18)
AiController.FixedUpdate () (at Assets/myAssets/scripts/AiController.cs:26)
Here is my code:
using UnityEngine;
using System.Collections;
public class AiController : MonoBehaviour
{
public string wayPoint = "wayPoint"; // Name of next waypoint
//private Transform target;
private GameObject target;
void turn()
{
}
void go()
{
target = GameObject.Find(wayPoint);//transform.Find("wayPoint");
transform.LookAt(target.transform);
rigidbody.AddRelativeForce(Vector3.forward/*left*/ * 200);
Debug.Log(target);
}
void FixedUpdate()
{
go();
}
}
any explanation?
Wow i feel stupid now. Whydoitdoit was right, it was all lowercase in the inspector (>.<). I have it in update because I will eventually be adding more random behavior to the AI so they swerve a bit and maybe go in the grass / crash depending on difficulty and weather they are supose to be "good" based on random number. I will change it so it is only doing look at a couple times a second and the rest only when it crosses a waypoint. Thanks for the help. Sorry whydoitdoit but it wont let me up vote your post.
– ZeroRadius