Issue with Respawnable Enemies' Spawning points

Hi all, I have an interesting problem. For some reason, I have two very special Enemy prefabs that Spawn at the red circle on my game map, instead of the two blue circles each enemy is supposed to be spawned at. I had them both deactivated off my scene for a while, as I wanted to rapidly test other things in my game, hence I probably did something wrong sometime down the line. They basically spawn based on a point in my game space, which is placed correctly (but the enemies still insist to spawn at the wrong spot for some reason). What could be the cause of this issue? (I did further testing, and realized most enemies spawn from a specific point in the scene, and I have no idea why)

Here is my RespawnManager.cs code (following Brian’s tutorial on that topic) that I use, if that helps in anyway:

using System.Collections.Generic;
using GameDevTV.Saving;
using RPG.Attributes;
using RPG.Control;
using UnityEngine;
using RPG.Combat;

namespace RPG.Respawnables
{
    public class RespawnManager : SaveableEntity
    {
        [SerializeField] AIController spawnableEnemy;
        [HideInInspector] private AIController lastSpawnableEnemy;
        [SerializeField] private float hideTime = 60;
        [SerializeField] private float respawnTime = 90;
        [SerializeField] PatrolPath patrolPath;
        // [SerializeField] AggroGroup aggroGroup; // aggrevated group of guards, based on wrong dialogue player has said

        private AIController spawnedEnemy;

        private void Awake()
        {
            Respawn();
        }

        private void Respawn()
        {

            /* if (aggroGroup != null) {

                aggroGroup.AddFighter(spawnedEnemy.GetComponent<Fighter>());

            } */

            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)
            
            {
                Debug.Log($"Assigning Patrol Path {patrolPath} to {spawnedEnemy.name}");
                spawnedEnemy.AssignPatrolPath(patrolPath);
            }

            else
            
            {
                Debug.Log($"No Patrol Path to assign");
            }

        }

        void HideCharacter()
        {
            foreach (Renderer renderer in spawnedEnemy.GetComponentsInChildren<Renderer>())
            {
                renderer.enabled = false;
            }
        }

        void OnDeath()
        {
            Invoke(nameof(HideCharacter), hideTime);
            Invoke(nameof(Respawn), respawnTime);

            /* if (aggroGroup != null) {

                aggroGroup.RemoveFighter(spawnedEnemy.GetComponent<Fighter>());

            } */

        }

        public void WaitingPeriod() {

            spawnedEnemy.SuspicionBehaviour();

        }

        public override object CaptureState()
        {
            var state = new Dictionary<string, object>();
            foreach (var saveable in spawnedEnemy.GetComponents<ISaveable>())
                state[saveable.GetType().ToString()] = saveable.CaptureState();
            return state;
        }


        public override void RestoreState(object state)
        {
            var stateDict = (Dictionary<string, object>)state;
            foreach (var saveable in spawnedEnemy.GetComponents<ISaveable>())
            {
                var typeString = saveable.GetType().ToString();
                if (stateDict.ContainsKey(typeString)) saveable.RestoreState(stateDict[typeString]);
            }

            if (spawnedEnemy.GetComponent<Health>().IsDead())
            {
                OnDeath();
            }
        }

        private void OnValidate()
        {
            if (spawnableEnemy != lastSpawnableEnemy)
            {
                lastSpawnableEnemy = spawnableEnemy;
                
                foreach (Transform child in transform) {

                    Destroy(child.gameObject);

                }

                Instantiate(spawnableEnemy, transform);

            }
        }
    }
}

Edit: They seem to be instantiating out of another character on the map. I went through the characters’ Prefab, and he doesn’t seem to have anything related to the two characters that are causing me the issue. What could be the problem?

You’re not placing the spawned enemy anywhere which means they will spawn at (0,0) of their parent transform. If they don’t appear there, then it is likely because their position in the prefab is not set to (0,0) and they are offset by whatever the position is in the prefab.

Interestingly enough, that’s not the case. The Spawn Point is set to (0,0.1,0) for the non-functional prefabs, and the functional prefabs as well. The only difference is, 2 of them know where to Spawn, and 3 of them literally are insisting on Spawning out of the ‘Quest Giver’ character’s position in my scene. I went through the Transforms multiple times, they’re all set to 0,0,0 and in their proper positions in the scene

They both want to instantiate from under my Quest Giver, which if I’m guessing correctly, is a Character Prefab-lined character, even though these two enemies are quite literally a fair distance apart in their Patrol paths

What does ‘spawning out of the ‘Quest Giver’’ mean? They should spawn where the RespawnManager game object is located

I am certain that that’s what they’re supposed to do, but that’s not what they’re doing right now for some reason. They both just instantiate at the Quest Giver’s NPC spot, and walk to their Patrolling paths from there, and I have no idea why

Does the quest giver spawn the enemies for a quest? Is it creating the RespawnManager objects?

I went through his hierarchy multiple times. I don’t think he’s spawning them, I just think they like spawning from his position for some reason.

I fixed the glitch, but it was a very weird fix, and I still have absolutely no idea why. In Brian Trotter’s Respawn Manager script, he called the ‘Respawn’ from the ‘Awake’ function, and Unity complained to me about it. I changed it to the ‘Start’ function, and for some unknown reason, the problem has been fixed

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms