Respawning enemies in the RPG course

Hi Brian. I did as you mentioned, but the Enemy does not move now. Yes, he fights back, dies and respawns, but he does not follow the Patrol Path. Overall, if I try make the Patrol Path as a prefab and assign it as my enemy goal in the AIController, he still won’t follow it. He’ll just stand idle, wait for me to approach the distance for him to pursue and fight me, fight and then normally respawn

The function works, but now he’s not patrolling. How do I solve this, and how can we make this as a working prefab for when we want to scatter them around the map?

That wasn’t in the original question… :stuck_out_tongue:

So that will be something that needs to be added to the RespawnManager. Create a serialized field for the Patrol Path in the Respawn Manager, along with a public method in AIController to assign the Patrol Path. When the character is spawned, if there is a Patrol Path in the spawner, then it should call the new AssignPatrolPath method on the AIController.

What are the contents of the AssignPatrolPath() function in the AIController? :smiley:

Can we use the ‘PatrolBehaviour()’ function in ‘AIController.cs’ instead?

No, the PatrolBehaviour acts on the PatrolPath, and is called from AIController.Update()

Your AssignPatrolPath() method should take a PatrolPath as a parameter, and assign that PatrolPath to the AIController’s patrolPath.

Hi Brian. I tried to make some changes, but it’s still not working. Here are the changes I have done to AIController.cs and RespawnManager.cs:

This is my new ‘AssignPatrolPath()’ function in ‘AIController.cs’ (the patrolPath was a serialized variable from before, when I was still in the Core Combat course):

     public void AssignPatrolPath(PatrolPath newPatrolPath) {
        newPatrolPath = patrolPath;
    }

And here is my Respawn() function addon (over the script I wrote along with you above), at the end of the function, right before it finishes:

if (patrolPath != null) {
                spawnableEnemy.AssignPatrolPath(patrolPath);
            }

What went wrong?

Take a closer look at that assignment statement in AssignPatrolPath… remember that our goal is to make the patrolPath take on the value in newPatrolPath…

I went through what you mentioned earlier, and tried to change the return type of my PatrolPath to be a PatrolPath, but it’s still not working. Prior to that, I thought it was a value transfer error, so I tried using ‘==’ instead of ‘=’, but that failed too. Here is my function, please let me know what we can do to fix that:

public PatrolPath AssignPatrolPath(PatrolPath newPatrolPath) {

        return newPatrolPath = patrolPath;

    }

You’re going to laugh when I give you the answer…

public void AssignPatrolPath(PatrolPath newPatrolPath)
{ 
    patrolPath = newPatrolPath;
}

ok honestly, I feel dumb right now for not seeing that, but the function still does not work. I think the issue is in my Spawn Function. I would appreciate if we can look through it together to see what went wrong:

private void Respawn()
        {
            if (spawnedEnemy)
            {
                spawnedEnemy.GetComponent<Health>().onDie.RemoveListener(OnDeath);
            }
            foreach (Transform child in transform)
            {
                Destroy(child.gameObject);
            }
            spawnedEnemy = Instantiate(spawnableEnemy, transform);
            spawnedEnemy.GetComponent<Health>().onDie.AddListener(OnDeath);

            if (patrolPath != null) {

                spawnableEnemy.AssignPatrolPath(patrolPath);

            }
        }

That actually looks correct.
Let’s throw in some debugs to see what’s going on.
(p.s. I’ll probably not get to the rest of your questions on Udemy tonight…)

We did some debugging. I got a few errors (that probably have nothing to do with the debugging itself), and here are the results I got so far:

Regarding the Udemy Questions, I’m okay with waiting till tomorrow, but we might want to prioritize my loot saving system failure when we switch scenes by then

Do you have six spawners?

You only account for about 1/2 of the Udemy questions tonight… ideally I try to answer all of them within 24.

I do have 4 enemies using the same prefab on the map (and the other two were clones under the Spawner Object Script I was prototyping with, which I just deleted). I am currently testing the system on one enemy though, I think the system is trying to assign the patrolpaths for all 4 of them, but the major issue is with the one I am trying things out with

I see the issue, you need to assign the Patrol Path to the spawnedEnemy, not the spawnableEnemy (the spawnedEnemy is the instance, and should be named Enemy (clone), the spawnableEnemy is the prefab. We don’t want to assign things to that.

Ahh, is it safe to say it’s still not working? I really do apologize if I am being a burden tonight. I did the changes in the if-else statement accordingly:

if (patrolPath != null)
            {
                Debug.Log($"Assigning Patrol Path {patrolPath} to {spawnedEnemy.name}");
                spawnedEnemy.AssignPatrolPath(patrolPath);
            }
            else
            {
                Debug.Log($"No Patrol Path to assign");
            }

So… the enemy doesn’t have a PatrolPath assigned after this? What are the Debugs saying? When you look in the inspector at the Enemy (clone) when the game is running, what’s the value in PatrolPath?

If it helps in anyway, I did once ask for help to randomize the waypoints of our patrolPaths. I’m not sure how that might be of help in our context, but I’m bringing it up just in case. Here is what the Enemy (Clone) says in the Debugging sector (along with the yellow errors mentioned earlier, in case they might be of help), and the enemy does have a PatrolPath Prefab assigned to him:

Then the issue with the PatrolPath is not here at this point… probably the randomized waypoint thing… that’s a separate topic, so we won’t address that here.
We can address my mistake in OnValidate, however. Rather than calling Respawn() in OnValidate, we’re going to do things a bit more manually…

        private void OnValidate()
        {
            if (spawnableEnemy != lastSpawnableEnemy)
            {
                lastSpawnableEnemy = spawnableEnemy;
                foreach (Transform child in transform)
                {
                    DestroyImmediate(child.gameObject);
                }
                Instantiate(spawnableEnemy, transform);
            }
        }

Fixed it (as in, I changed the ‘OnValidate()’ function to follow yours), but the issue of the patrolPath still persists. I don’t honestly think the issue is with the Random Waypoint index either. I went and tried changing my Cycle of the waypoints back to follow the code Sam provided us, and the issue still persisted.

Is the Patrol behaviour working correctly on a normal enemy? One that starts in the game, not under a SpawnManager?

Privacy & Terms