I was playing my game while the profiler was on and i noticed a frame drop and a spike on the Profiler, that seem to come from my respawner script.
The spike on the profiler
The info about the spike
Here’s the script:
using System.Collections;
using UnityEngine;
public class Respawner : MonoBehaviour {
//=============//
//=[VARIABLES]=//
//=============//
//Static variables
public static Respawner current;
//Cache variables
WaitForSeconds timeToRespawn = new WaitForSeconds(5f);
ChooseSpawn chosenSpawn = new ChooseSpawn();
//==================//
//=[INITIALIZATION]=//
//==================//
//Initialize the static variable
void Awake()
{
current = this;
}
//===================//
//=[RESPAWN METHODS]=//
//===================//
//Respawn an AI
public IEnumerator RespawnAI(GameObject aiToRespawn,bool delaySpawn)
{
//Wait for 5 seconds
if (delaySpawn)
yield return timeToRespawn;
//Choose a random spawn
chosenSpawn.Initialize(aiToRespawn.tag);
//Spawn AI
aiToRespawn.transform.position = chosenSpawn.position;
aiToRespawn.transform.rotation = chosenSpawn.rotation;
aiToRespawn.SetActive(true);
//Reset some stats
aiToRespawn.GetComponent<MasterPlayerScript>().ResetStats();
yield return null;
}
//Respawn a player
public IEnumerator RespawnPlayer(GameObject player,bool delaySpawn)
{
//Wait for 5 seconds
if (delaySpawn)
yield return timeToRespawn;
//Choose a random spawn
CameraScript camera = CameraScript.current;
chosenSpawn.Initialize(player.tag);
//Spawn player and set camera
player.transform.position = chosenSpawn.position;
player.transform.rotation = chosenSpawn.rotation;
player.SetActive(true);
camera.target = player.transform;
//Reset the stats of the player
player.GetComponent<MasterPlayerScript>().ResetStats();
}
//===========//
//=[CLASSES]=//
//===========//
//This class will output the spawns rotation and a random point around the spawn
public class ChooseSpawn
{
public Vector3 position;
public Quaternion rotation;
//Initialize
public void Initialize(string team)
{
//Chooses a ranodm spawn
GameObject[] spawns = GameObject.FindGameObjectsWithTag(team + " Spawn");
Transform chosenSpawn = spawns[Random.Range(0, spawns.Length)].transform;
//Chooses a random point around the spawn
Vector2 randomOnUnitCircle = Random.insideUnitCircle * 3 + new Vector2(chosenSpawn.position.x, chosenSpawn.position.z);
//Sets values
position = new Vector3(randomOnUnitCircle.x, chosenSpawn.position.y, randomOnUnitCircle.y);
rotation = chosenSpawn.rotation;
}
}
}