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 ();
playerAudio = GetComponent ();
playerMovement = GetComponent ();
playerShooting = GetComponentInChildren ();
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][1]
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!
[1]: https://answers.unity.com/questions/866963/mystery-restartlevel-function-used-in-official-tut.html
,
↧