Creating prefab through Zenject Factory makes navMeshAgent act strange
I am newcomer for DI and Zenject in particular. Here is my code for creating a truck:
public class TruckControl : MonoBehaviour
{
[SerializeField]
private NavMeshAgent agent;
[Inject]
PlantControl plantControl; //The reference to the class which creates my truck. If I remove it, NullReferenceException will occur
[Inject]
public readonly SignalBus signalBus;
[Inject]
public void Construct(Vector3 spawnPoint, Vector3 finishPoint)
{
agent.Warp(spawnPoint);
agent.destination = finishPoint;
}
public void StopMovement()
{
if (!agent.isStopped)
agent.isStopped = true;
}
public void ResumeMovement() //this methods is called from another class using signals
{
if (agent.isStopped)
agent.isStopped = false;
}
private void OnTriggerEnter(Collider other)
{
ILoadGoodable bullDozerLoad = other.gameObject.GetComponent<ILoadGoodable>();
if (bullDozerLoad != null && !bullDozerLoad.IsVisited)
{
StopMovement();
signalBus.Fire<StartLoadingGoodsInTruck>();
}
}
public class TruckFactory : PlaceholderFactory<Vector3, Vector3, TruckControl> {}
}
I don't know why, but he throws an exception: "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
And here is a pickle: if I place this lines
agent.Warp(spawnPoint);
agent.destination = finishPoint;
into Start() method, everything works fine, until ResumeMovement() method, which, as mentioned above, is called from another class using Zenject signals. What am I doing wrong here?
Your answer
Follow this Question
Related Questions
Having problems with making a Navmesh Agent stop and play an attack animation 0 Answers
problems with nav mesh agent 0 Answers
How to get the closest object in the navMesh and making the agent move towards it 1 Answer
Change NavMeshAgent Speed With Script? 1 Answer
Object reference not set to an instance of an Object? 0 Answers