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)
You need Start() and Update(), you have them lowercase.
nice response , but it still not working and now im having the error NullReferenceException: Object reference not set to an instance of a object
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.
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
Please use code tags as requested, I can’t read it. And share the time in the video where the code is shown.
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?
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
void Start()
{
Debug.Log("I'm in Start!");
}
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
"Character" code
public class Character : MonoBehaviour {
public float speed;
private Waypoints Wpoints;
private int waypointIndex;
void Start()
{
Wpoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoints>();
}
void Update() {
transform.position = Vector2.MoveTowards(transform.position, Wpoints.waypoints[waypointIndex].position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, Wpoints.waypoints[waypointIndex].position) < 0.1f){
waypointIndex++;
}
}
}
"Waypoints" code
public class Waypoints : MonoBehaviour {
public Transform[] waypoints;
}
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.
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
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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour {
public float speed;
private Waypoints Wpoints;
private int waypointIndex;
void Start()
{
Wpoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoints>();
}
void Update() {
transform.position = Vector2.MoveTowards(transform.position, Wpoints.waypoints[waypointIndex].position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, Wpoints.waypoints[waypointIndex].position) < 0.1f){
waypointIndex++;
}
}
}
Did you remember to add the Waypoints tag to the object as mentioned in the error?
i recently discovered that i put the wrong scrip in waypoints but i still have 2 errors left
Can you answer my last question?
yes i did i added the waypoints tag to the object
The error says you did not, please show a screenshot.
Regardless of code, have you baked NavMesh?
sorry for respoding late, heres the screenshot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour {
public float speed;
private Waypoints Wpoints;
private int waypointIndex;
void Start()
{
Wpoints = GameObject.FindGameObjectWithTag("Waypoints").GetComponent<Waypoints>();
}
void Update() {
transform.position = Vector2.MoveTowards(transform.position, Wpoints.waypoints[waypointIndex].position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, Wpoints.waypoints[waypointIndex].position) < 0.1f){
waypointIndex++;
}
}
}