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