Script / Start() and Update() not being called
Hi There,
I have a script for an enemy with a NavMeshAgent to patrol diferent points and for some reason it won't work. When adding in Breakpoints in the code to find out what was happening, I discovered that neither Start() nor Update() were even being called! I have no idea why this is happening and before you ask: I have DEFINETLEY attached the script to the Enemy, There is DEFINITELY a NavMeshAgent on the enemy, I have DEFINETLY ticked the script on, and so on. I have checked absolutely everything and it still won't work. Please help if you can.
Code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class SimplePatrol : MonoBehaviour { [SerializeField] float _totalWaitTime = 3.0f;
[SerializeField]
bool _patrolWaiting;
[SerializeField]
float _switchProbability = 0.2f;
[SerializeField]
List<Waypoint> _patrolPoints;
NavMeshAgent _navMeshAgent;
int _currentPatrolIndex;
bool _travelling;
bool _waiting;
bool _patrolForward;
float _waitTimer;
// Use this for initialization
void Start()
{
_navMeshAgent = this.GetComponent<NavMeshAgent>();
if (_navMeshAgent == null)
{
Debug.LogError ("The nav mesh component is not attached");
}
else
{
if (_patrolPoints != null && _patrolPoints.Count >= 2)
{
_currentPatrolIndex = 0;
SetDestination();
}
else
{
Debug.Log("Insufficent patrol points");
}
}
}
// Update is called once per frame
void Update()
{
if (_travelling && _navMeshAgent.remainingDistance <= 1.0f)
{
_travelling = false;
if (_patrolWaiting)
{
_waiting = true;
_waitTimer = 0f;
}
else
{
ChangePatrolPoint();
SetDestination();
}
}
if (_waiting)
{
_waitTimer += Time.deltaTime;
if (_waitTimer >= _totalWaitTime)
{
_waiting = false;
ChangePatrolPoint();
SetDestination();
}
}
}
private void SetDestination()
{
if (_patrolPoints != null)
{
Vector3 targetVector = _patrolPoints[_currentPatrolIndex].transform.position;
_navMeshAgent.SetDestination(targetVector);
_travelling = true;
}
}
private void ChangePatrolPoint()
{
if (UnityEngine.Random.Range(0f, 1f) <= _switchProbability)
{
_patrolForward = !_patrolForward;
}
if (_patrolForward)
{
_currentPatrolIndex = (_currentPatrolIndex + 1) % _patrolPoints.Count;
}
else
{
if (--_currentPatrolIndex < 0)
{
_currentPatrolIndex = _patrolPoints.Count - 1;
}
}
}
}
Answer by theterrificjd · Dec 14, 2017 at 12:37 AM
Try defining public vs private in your global variable declarations ie:
public float _totalWaitTime = 3.0f
[SerializeField]
public bool _patrolWaiting;
[SerializeField]
public float _switchProbability = 0.2f;
[SerializeField]
public List<Waypoint> _patrolPoints;
private NavMeshAgent _navMeshAgent;
private int _currentPatrolIndex;
private bool _travelling;
private bool _waiting;
private bool _patrolForward;
private float _waitTimer;
I'm not certain if that would be causing this issue, but it shouldn't hurt.
Public fields are serialized by default no need to use [SerializeField] for them!
Answer by adriant · Dec 19, 2017 at 08:28 PM
Running the script you posted I was able to step into breakpoints just fine. I was testing with Visual Studio 2015 on Windows. Which IDE do you use? If you use Visual Studio please make sure you have Visual Studio Tools for Unity properly installed. https://marketplace.visualstudio.com/items?itemName=SebastienLebreton.VisualStudio2015ToolsforUnity
Are you able to step into any other scripts?
I have to mention that I replaced List<Waypoint>
with List<GameObject>
in the script in order to make it compile in my test project and to have it working. Then I had a couple of objects added in to the list and the agent was patrolling between the game objects' positions.