I keep getting a NullReferenceException error when I hit play. It tells me that my target is not set to a reference object but it is set to a transform object. I created an empty game object, named it Waypoint and set it as the target, the Character should be moving towards the Waypoint but it just tells me
“NullReferenceException: Object reference not set to an instance of an object
AIcharacter.Update () (at Assets/AIcharacter.cs:31)”
I was having another problem with C# scripts yesterday and the only fix was copying the same exact script, then deleting the script, making a new one with the same name and pasting the old script for some odd reason… I’m new to programming altogether but especially new to C#. Here is the Script in question
using UnityEngine;
using System.Collections;
public class AIcharacter : MonoBehaviour {
NavMeshAgent agent;
CharacterMovement CharMove;
public Transform target;
float targetTolerance = 1;
Vector3 targetPos;
// Use this for initialization
void Start () {
agent = GetComponentInChildren<NavMeshAgent>();
CharMove = GetComponent<CharacterMovement>();
}
// Update is called once per frame
void Update () {
if (target != null)
{
if((target.position - targetPos).magnitude > targetTolerance)
{
targetPos = target.position;
agent.SetDestination(targetPos);
}
agent.transform.position = transform.position;
CharMove.Move(agent.desiredVelocity);
}
else
{
CharMove.Move(Vector3.zero);
}
}
}
There are no build errors and all other scripts are functioning properly.
Ive looked over it and it looks identical to the script in the tutorial I am following.
BTW, I am using Unity 4.6 because the tutorial I am following calls for that version.
Any help would be appreciated.
1 Like
It’s not complaining about the target, it’s complaining about the agent. It gives the error for line 31, which is this line:
agent.transform.position = transform.position;
Are you sure you’ve added a NavMeshAgent to a child of your game object? Did you intend to use GetComponent and not GetComponentInChildren?
1 Like
It is the same as the tutorial. He creates an empty object in the scene, names it NavMeshAgent and assigns a Nav Mesh Agent component to it, then he creates another empty object, names it Waypoint, and drags it to the target field in the AIcharacter script, then he bakes the navigation to the level objects. disables UserInput script, enables AIcharacter script, and when he clicks play the character follows the Waypoint as he moves it. I’ve done the exact same things, but no luck.
I just tried using GetComponent instead but only got a new error
“MissingComponentException: There is no ‘NavMeshAgent’ attached to the “Third Person Controller” game object, but a script is trying to access it.
You probably need to add a NavMeshAgent to the game object “Third Person Controller”. Or your script needs to check if the component is attached before using it.
UnityEngine.Component.get_transform () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineComponent.cs:21)
AIcharacter.Update () (at Assets/AIcharacter.cs:31)”
I tried parenting the Third Person Controller to the NavMeshAgent object but got the same error, then I tried adding a Nav Mesh Agent component to the controller object instead and the error was gone but it still did not try to go to the target.
I had a weird problem yesterday that for whatever reason, would not accept GroundCheck until I copied and pasted the same code to a new CS script with the same name, I’ve never had these type of problems with Java, but I really want to work with C#.
I can paste the script that tells the character how to handle movement if that might help, but I don’t think it would, my input script works fine with it.
Edit: I typed GetComponentInChildren back in its place, added the NavMeshAgent object to the controller object, the error gone but the problem still exists. I rewatched that part of the tutorial, and I noticed he was creating the navmesh object with the controller selected. But yes it should have been children to the controller object, maybe I’m a little closer to fixing this issue, thanks for pointing that out Grozzler
1 Like
I cleared the navigation, baked it again, adjusted the y axis of the Waypoint and the NavMeshAgent and have gotten results, now he runs in place towards the Waypoint, I am pretty sure it has to do with the NavMeshAgent clipping through the floor, if I adjust it, it just keeps snapping it back through the floor but pretty sure I can get it sorted out now since lowering the plane works just fine. Consider this problem resolved, thanks again.
1 Like