- Home /
How do I remove the automatic restart function from the Survival Shooter tutorial series?,
Hello,
I am aiming to remove the automatic restart capability because I want a menu to pop up with the "Game Over" text with two button saying "Try Again" and "Menu". I followed the tutorial and its updated pdf guide for Unity 5+, for reference, I am using Unity 5.6.6f2.
There is a block of code that I think controls the restart ability:
public void RestartLevel ()
{
SceneManager.LoadScene(0);
}
This is located at the bottom of my PlayerHealth script, I see nothing that would be linked or do anything with the restart sample above, but you all know more than me for sure so here is my full script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public Slider healthSlider;
public Image damageImage;
public AudioClip deathClip;
public float flashSpeed = 5f;
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
Animator anim;
AudioSource playerAudio;
PlayerMovement playerMovement;
PlayerShooting playerShooting;
bool isDead;
bool damaged;
void Awake ()
{
anim = GetComponent <Animator> ();
playerAudio = GetComponent <AudioSource> ();
playerMovement = GetComponent <PlayerMovement> ();
playerShooting = GetComponentInChildren <PlayerShooting> ();
currentHealth = startingHealth;
}
void Update ()
{
if(damaged)
{
damageImage.color = flashColour;
}
else
{
damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damaged = false;
}
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
playerAudio.Play ();
if(currentHealth <= 0 && !isDead)
{
Death ();
}
}
void Death ()
{
isDead = true;
playerShooting.DisableEffects ();
anim.SetTrigger ("Die");
playerAudio.clip = deathClip;
playerAudio.Play ();
playerMovement.enabled = false;
playerShooting.enabled = false;
}
public void RestartLevel ()
{
SceneManager.LoadScene(0);
}
}
Again, there are some adjustments in the script not present in the original script from the tutorial series on Unity's channel due to the upgraded guide for Unity 5+. I saw another question that is related to my issue, but the answer simply located the restart function with the player death animation I believe.
The question is here
When I try to remove the block of code that restarts the level I get this error:
'Player' AnimationEvent 'RestartLevel' has no receiver! Are you missing a component?
Finally, when the scene does restart, the preview of my game has darker lighting than it did before it restarted, when I exit out of play mode the lighting is back to normal. This happens every time. What am I doing wrong and how can I do the manual restart mentioned in the beginning? I'm fairly new to Unity and this is for my school project that will be on display!
,
Answer by cdr9042 · Jan 04, 2019 at 03:20 AM
If you've found out where the animation event is then skip the next paragraph.
That restart function is called from an AnimationEvent. Check the player object, it has an AnimatorController component, open the Animation window and click on the player object, now in the Animation window there is a drop down list under the play, record... buttons. Check for the dead animation clip. The animation event is probably somewhere in there, it's a little vertical dash in the animation timeline
To change the function called by the event, click on it and change it in the Inspector window. To delete it, right click and delete.
To manually restart your game, make a UI object popup in the animation event (create a disabled object then active it is one way to make this UI object pop up), inside that object, have UI Buttons in there with OnClick() set to the function you want to call when it's clicked, in this case, RestartLevel()
I figured it out an O $$anonymous$$ solution, I just decided to omit the ability to have the restart and main menu button, now when the player dies they go to the main menu scene (did not have one prior to posting, when I saw this interaction I liked it).
Although, I did try your suggested fix, and it worked!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Space Shooter: Ending the Game 0 Answers