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?