Path is not in order

As per title, the method FindPath looks for the GameObjects with the tag “Path” but it doesn’t place it into the list in order. As a result, the Ram goes all over the place attempting to follow the waypoint in the wrong order.

image

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyMover : MonoBehaviour
{
    [SerializeField] List<Waypoint> path = new List<Waypoint>();
    [SerializeField] [Range(0f, 5f)]float moveSpeed = 1f;

    // Start is called before the first frame update
    void Start()
    {
        FindPath();
        StartCoroutine(FollowPath());
    }

    void FindPath()
    {
        path.Clear();

        GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path");

        foreach(GameObject waypoint in waypoints)
        {
            path.Add(waypoint.GetComponent<Waypoint>());
        }
    }

    IEnumerator FollowPath()
    {
        foreach(Waypoint waypoint in path)
        {  
            Vector3 startPosition = transform.position;
            Vector3 endPosition = waypoint.transform.position;
            float travelPercent = 0f;

            transform.LookAt(endPosition);

            while(travelPercent < 1f)
            {
                travelPercent += Time.deltaTime * moveSpeed;
                transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
                yield return new WaitForEndOfFrame();
            }
        }
    }
}

Hi CookEasy,

Welcome to our community! :slight_smile:

That’s indeed odd. Have you already checked the order of the waypoints in the ‘Path’ game object in your Hierarchy? Your code does nothing but looking for that object, then iterating over the children and adding each child to the path. It does not change the order. For this reason, it is likely that the issue is not in your code but in the Scene.

Thank you for the response Nina!

The gameobjects under path are ordered correctly as shown on the image below.
image

Unfortunately I’m not sure as to where to start troubleshooting because as you said, the code simply runs the foreach loop to add the GameObjects to the list.

Does the Path game object look the same at runtime?

It is very odd that the the order is completely changed in your EnemyMover. What happens if you stop the game, start it again, stop it, start it again, and so on? Do you get the same order or is it a random order?

For testing purposes, add a Debug.Log to your foreach loop block. Maybe like this:

foreach(GameObject waypoint in waypoints)
{
    Debug.Log("Child name: " + waypoint.name, waypoint);
    path.Add(waypoint.GetComponent<Waypoint>());
}

When clicking on the messages once, the corresponding waypoint should get highlighted in your Hierarchy. Maybe there is another game object, and your code uses that one instead of the Path we are looking at.

Add Debug.Log("Path game object: ", path.gameObject); to your code. Due to the second parameter, you are able to click the message to get the Path highlighted in your Hierarchy. Of course, this works only after the ‘Path’ game object was found.

Yep the path looks similiar at runtime. The list is also arranged in the same order everytime.

I added the Debug.Log code to the foreach loop and here is what comes up in the terminal.
image

Clicking on the messages in the terminal highlights the same GameObjects in Path. I clicked on all the messages and it only highlights the existing GameObjects in the hierarchy.
image

EDIT: I found something strange on the behaviour of the script.
I basically deleted all the tiles with the tag “Path” from the hierarchy and put it back again. Now, after running the scene, the order of the list has become reversed, starting from the last placed tile. The order in the hierarchy is still the same.
image
image

try it like this
image

Hello bryan, I’m on the part of the lecture where instead of dragging to the script to create a list, we’re using code to tag objects as “path” which is then added to the list, I have no problems with manually adding them, it’s the automated path that doesn’t work for some reason.

Something strange is going on in your project. :confused:

Could you please fill out our form and send me your zipped project (without the Temp and Library folders)? I’d like to take a look into this. You don’t have to write a lenghty description of the problem in the form. Your name, my name, the project and a link to this thread are sufficient.

When you are done, drop me a line here, so I can check our server.

Sent the form with the attached zip file, thank you!

Thanks. I’ve just taken a look into your project with Unity 2022.3.1f1 but was not able to recreate your problem.

At runtime, I’m getting this:

image,

which corresponds with this:

image

Close Unity and delete the Library folder. Start Unity again, open your scene and test your game again. If you are lucky, there was just an internal problem in Unity. If not, consider either updating or downgrading.

Before updating or downgrading your project, make a backup of your project folder by duplicating it. Save the duplicated folder somewhere else, maybe on an USB stick. To save some free space, you could delete the Library and Temp folders in the backup folder.

Or did I do anything ‘wrong’? If so, please let me know step by step what I have to do to get the same problem as you.

Privacy & Terms