I'm going nuts for 2 days about NullReferenceException error please help!
Hello guys. My main character will pick an object "Pick ups" and Police will chase him(AIThirdpersonController). I couln't do it no matter what. Plase take a look at my codes. I'm trying to acces a function from another one. Thank you
Thats my PlayerController script on my main character(Wilson);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
using UnityStandardAssets.Characters.ThirdPerson;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
AICharacterControl PoliceRun;
GameObject Winston;
private GameObject police;
public int count;
private Rigidbody rb;
Transform target;
private Vector3 place;
string count1;
// Use this for initialization
void Start() {
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
police = GameObject.FindWithTag("Police");
Winston = GameObject.FindWithTag("Winston");
PoliceRun = GameObject.FindWithTag("Police").GetComponent<AICharacterControl>();
Debug.Log(target.ToString());
}
// Update is called once per frame
void Update() {
}
private void FixedUpdate()
{
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag("Pick Up")) ;
{
other.gameObject.SetActive(false);
count = count + 1;
SetTargetAI();
}
}
void SetCountText() {
count1 = countText.text;
countText.text = "Collected=" + count.ToString();
if (count >= 12 )
{
winText.text = "WIN!";
}
}
void SetTargetAI()
{
PoliceRun.SetTarget(Winston.transform);
}
}
And Thats AICharacterControl (police) ;
using System;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof(UnityEngine.AI.NavMeshAgent))]
[RequireComponent(typeof(ThirdPersonCharacter))]
public class AICharacterControl : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
public ThirdPersonCharacter character { get; private set; } // the character we are controlling
public Transform target; // target to aim for
PlayerController pc;
Transform WinstonT;
private void Start()
{
// get the components on the object we need ( should not be null due to require component so no need to check )
agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
agent.updateRotation = false;
agent.updatePosition = true;
pc = GameObject.FindWithTag("Police").GetComponent<PlayerController>();
WinstonT = GameObject.FindWithTag("Winston").transform;
}
private void Update()
{
if (target != null)
agent.SetDestination(target.position);
if (agent.remainingDistance > agent.stoppingDistance)
character.Move(agent.desiredVelocity, false, false);
else
character.Move(Vector3.zero, false, false);
}
public void SetTarget(Transform target)
{
this.target = target;
}
}
}
I'm getting this error.
NullReferenceException: Object reference not set to an instance of an object PlayerController.SetTargetAI () (at Assets/Scripts/PlayerController.cs:74) PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:53
Did you try adding a breakpoint to see what is null?
I moved your published answer to be a comment under the question as it does not fulfill the requirements to be an answer - it does not attempt to answer the question. This is a comment, not an answer.
Please ensure that, moving forward, your answers are indeed that - answers, not comments on the matter of the question.
Answer by UnityCoach · May 11, 2017 at 11:24 AM
The only thing I see is that one the private members were not assigned. Try adding the following checks.
police = GameObject.FindWithTag("Police");
Winston = GameObject.FindWithTag("Winston");
PoliceRun = GameObject.FindWithTag("Police").GetComponent<AICharacterControl>();
if (!police)
Debug.LogWarning ("police not found");
if (!Winston)
Debug.LogWarning ("Winston not found");
if (!PoliceRun)
Debug.LogWarning ("PoliceRun not found");
Also ensure that you're actually setting the Tag to "Winston" - not "winston" or any other tag. Case sensitivity is important.