So iv been searching for ages and couldnt find anything to help yet so I must have missed something. Im trying to run a function for my ai to move to a different target when a bool is set to true and a collision is detected. The ai will move just fine with a button press from its own ai move script, but when trying to run the function from another script it gives me an error. Please help lol.
Also im not great at coding so please try to ignore if i have terrible code lol, still pretty new to c#
The function im trying to run is the moveToChair from AINavMesh.
ServeMat
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ServeMat : MonoBehaviour
{
private bool itemOnMat = false;
public AINavMesh aiNav;
private void Awake()
{
aiNav = GameObject.FindGameObjectWithTag("needed").GetComponent<AINavMesh>();
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Can_cola1")
{
Debug.Log("collison detected");
itemOnMat = true;
}
}
private void OnMouseDown()
{
Debug.Log("clicked");
if (GameFlow.orderValue == GameFlow.matValue && itemOnMat == true)
{
Debug.Log("correct");
aiNav.moveToChair();
}
}
}
AINavMesh
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AINavMesh : MonoBehaviour
{
[SerializeField] private Transform movePosTransformBar;
[SerializeField] private Transform movePosTransformChair;
private NavMeshAgent NavMeshAgent;
private bool orderComplete = false;
private void Awake()
{
NavMeshAgent = GetComponent<NavMeshAgent>();
NavMeshAgent.destination = movePosTransformBar.position;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
orderComplete = true;
moveToChair();
}
}
public void moveToChair()
{
NavMeshAgent.destination = movePosTransformChair.position;
}
}