Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ender2772 · Dec 13, 2017 at 07:34 PM · updatenavmeshstart

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;
         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pekaram · Feb 18, 2018 at 01:13 PM 0
Share

Public fields are serialized by default no need to use [SerializeField] for them!

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adriant · Dec 20, 2017 at 11:01 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where have the tutorials gone? 0 Answers

navmesh agent not working after update to 2017.1 3 Answers

transform position not working on start() only on update() 1 Answer

Start and Update dont work and checkmark for enabling/disabling script has disappeard 1 Answer

NavmeshAgent too far away after Unity Update 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges