Hello, how can i make callback in C# which is called on player respawn? For example i have teleport script where player is teleported on spawn with decrease value for lives by 1. On this step i want callback for example OnPlayerRespawn which i can call from another script. I need this to set SetActive to true for certain object which player destroy and on respawn i want to setactive to true back. Thanks a lot for your help and time.
Yea, thanks, events, that what i looking for. But now i must learn how can i make this eventmanager to call my function on specific time (onrespawnplayer)
i create in TeleportScript this
public class CollisionStateTeleportToStart : AbstractClass
{
public delegate void RespawnAction();
public static event RespawnAction OnRespawn;
and in specific line i put OnRespawn() in teleportscript and on another script where i want to use event, it looks like
void Test()
{
SceneManager.LoadScene("LevelSelector");
}
private void OnEnable()
{
CollisionStateTeleportToStart.RespawnAction += Test;
}
private void OnDisable()
{
CollisionStateTeleportToStart.RespawnAction -= Test;
}
but error is CollisionStateTeleportToStart.RespawnAction is a type but its used like a variable. How to use it properly please? im working firs time with events
… big mistake, i call type as error said
i need to call OnRespawn. It works now
thanks a lot
but one question more, why sometimes my script throw nullreferenceexception when i call OnRespawn() in script where i define event?
NullReferenceException: Object reference not set to an instance of an object
CollisionStateTeleportToStart.TeleportToStart () (at Assets/Scripts/Collision/CollisionStateTeleportToStart.cs:221)
public void TeleportToStart()
{
OnRespawn();
restartCount++;
textScore.text = restartCount.ToString();
transform.position = spawnLocation;
kamera.position = CameraRestartPoint;
foreach (MoveObject item in moveObject)
{
item.ResetPositions();
}
foreach (var item in restartOnDeath)
{
line 221 is OnRespawn();
problem solved via checking if OnRespawn != null and add listener to OnStart and not in OnEnable;