Search Unity

Why enemy ia wont move?

Discussion in 'Navigation' started by justaunityuseridk, Aug 30, 2022.

  1. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    hi , im very new to unity and recently i have been triying to make a tower defense game in 3d and i got stuck in the ia movement , i have been trying from diferent tutorial but i didnt work , i try every tutorial step by step but the ia wont move! and the console shows no errors! even with waypoints, if someone professional knows how to fix this please respond(code below)
     

    Attached Files:

  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You need Start() and Update(), you have them lowercase.
     
  3. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    nice response , but it still not working and now im having the error NullReferenceException: Object reference not set to an instance of a object
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please show your updated code formatted with Code Tags so the line numbers show, and the exact error message which will show the line number for the error. Please take a few minutes to review other posts on here to see how the code is formatted with code tags, it's the Code: button in the edit ribbon bar above. Also please show the tutorial that you are using, and if in a video, show the location in the video that shows the code.
     
  5. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    hi , thanks for respoding , heres the video that i was using in the exact minute where the code is show:

    Screenshots(i got more errors when taking the photo(also visual studio says that there are no problems identified Screenshot_4.png Screenshot_6.png Screenshot_5.png
     

    Attached Files:

  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please use code tags as requested, I can't read it. And share the time in the video where the code is shown.
     
  7. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    look , im gonna be honest with you , i dont even know how to use code tags , can you please explain to me how to do it or send me a tutorial?
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I explained earlier, use the Code: button in the edit ribbon bar just above where you are typing. And please answer all questions instead of back and forth. I had asked for the time in the video, it looks like you are missing Waypoint.cs

    Code (CSharp):
    1. void Start()
    2. {
    3.    Debug.Log("I'm in Start!");
    4. }
     

    Attached Files:

    Le9o likes this.
  9. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    hi , thanks again for responding , i have tried putting the link in the exact minute where the code is shown but when i post the reply it converts into a video and it does not show the exact minute where the code is shown but instead at the start of the video , anyways heres the the time stand where the code is shown in the video below: 5:13


    Code (CSharp):
    1.       "Character" code
    2.     public class Character : MonoBehaviour {
    3.     public float speed;
    4.     private Waypoints Wpoints;
    5.  
    6.     private int waypointIndex;
    7.     void Start()
    8.     {
    9.  
    10.         Wpoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoints>();
    11.     }
    12.  
    13.     void Update() {
    14.  
    15.         transform.position = Vector2.MoveTowards(transform.position, Wpoints.waypoints[waypointIndex].position, speed * Time.deltaTime);
    16.         if (Vector2.Distance(transform.position, Wpoints.waypoints[waypointIndex].position) < 0.1f){
    17.             waypointIndex++;
    18.  
    19.     }
    20.   }
    21. }
    22.  
    23.       "Waypoints" code
    24.  
    25. public class Waypoints : MonoBehaviour {  
    26. public Transform[] waypoints;
    27. }
    28.  
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Got it, 5:13. As I mentioned, do you have Waypoint.cs in your project? Where are you seeing an error? Make sure to save all your files.
     
  11. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    yes, i do have waypoint.cs in my project.
    after i save what i made in visual studio(i have visual studio as external editor) the console is clear, but when i press to start the game, nothing happens, however after doing that and returning to the console are these following errors(also in the video the time stamp where Waypoint.cs is 2:30)

    looks like they re from the waypoints and the enemy
     

    Attached Files:

  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    The error line numbers (14 and 19) do not match with the line numbers that you posted above. Please restart Unity and Visual Studio and post your code again. But this time, don't add any comments so the line numbers match up.
     
  13. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Character : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     private Waypoints Wpoints;
    9.  
    10.     private int waypointIndex;
    11.     void Start()
    12.     {
    13.  
    14.         Wpoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoints>();
    15.     }
    16.  
    17.     void Update() {
    18.  
    19.         transform.position = Vector2.MoveTowards(transform.position, Wpoints.waypoints[waypointIndex].position, speed * Time.deltaTime);
    20.         if (Vector2.Distance(transform.position, Wpoints.waypoints[waypointIndex].position) < 0.1f){
    21.             waypointIndex++;
    22.  
    23.     }
    24.   }
    25. }
     
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Did you remember to add the Waypoints tag to the object as mentioned in the error?
     
  15. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    i recently discovered that i put the wrong scrip in waypoints but i still have 2 errors left
     

    Attached Files:

  16. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Can you answer my last question?
     
  17. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    yes i did i added the waypoints tag to the object
     
  18. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    The error says you did not, please show a screenshot.
     
  19. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    1,759
    Regardless of code, have you baked NavMesh?
     
  20. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    sorry for respoding late, heres the screenshot

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Character : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     private Waypoints Wpoints;
    9.  
    10.     private int waypointIndex;
    11.  
    12.     void Start()  
    13.     {
    14.  
    15.         Wpoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoints>();
    16.     }
    17.  
    18.     void Update() {
    19.  
    20.         transform.position = Vector2.MoveTowards(transform.position, Wpoints.waypoints[waypointIndex].position, speed * Time.deltaTime);
    21.         if (Vector2.Distance(transform.position, Wpoints.waypoints[waypointIndex].position) < 0.1f){
    22.             waypointIndex++;
    23.  
    24.     }
    25.   }
    26. }
     

    Attached Files:

  21. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Again, please answer our questions. I had asked if you added the "Waypoints" Tag to the object in the Inspector. You were also asked if you baked the navmesh.
     
    warthos3399 likes this.
  22. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    sorry for not respoding before, im busy doing other things, anyways heres the screenshot:
    oh and also i think i did not baked navmesh, maybe thats the error?
     

    Attached Files:

  23. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    1,759
    For AI to move you must bake NavMesh (it tells the agent where and not where it can move), if you dont it can be the cause of errors, as the NavMesh Agent is trying to move, but doesnt have a NavMesh to base its functions with.

    Yes, if NavMesh isnt baked, it will cause those errors...
     
  24. justaunityuseridk

    justaunityuseridk

    Joined:
    Aug 30, 2022
    Posts:
    12
    and how do i bake NavMesh so my agent can move?
     
  25. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    1,759
    Go to Window/AI/Navigation, it will open in the Inspector. At the top click on Bake, then at bottom right click the bake button.