Calling function in Update only once. I got the other scripts working (same function) but not this one.
Okay so I want to call my gameOver script in my EmptyClass.cs through the Update(checking if the player is dead) but the function I put in the Coroutine is repeatedly called so the gameOver sound is played repeatedly. The weird thing is the Update Function in my TimeManager.cs calls the same function just fine (run only once). Btw my EmptyClass.cs is a subclass of the Character.cs superclass and their codes are written below
I dealt with some problems in this game but not this one. Any help will be appreciated.
using UnityEngine; using System.Collections;
public delegate void DeadEventHandler();
public class EmptyClass : Character {
private static EmptyClass instance;
public event DeadEventHandler Dead;
public static EmptyClass Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<EmptyClass> ();
}
return instance;
}
}
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private float jumpForce;
[SerializeField]
private bool airControl;
[SerializeField]
private LayerMask whatIsGround;
public Rigidbody2D MyRigidbody { get; set; }
public bool Jump { get; set; }
public bool OnGround { get; set; }
private NewEnemy enemy;
private SuccessFail successfail;
private MenuCtrl menucontroller;
private bool playerdeadfail;
public override void Start ()
{
enemy = GameObject.FindObjectOfType<NewEnemy> ();
successfail = GameObject.FindObjectOfType<SuccessFail> ();
menucontroller = GameObject.FindObjectOfType<MenuCtrl> ();
MyRigidbody = GetComponent<Rigidbody2D> ();
base.Start ();
}
void FixedUpdate ()
{
if (!TakingDamage && !isDead) {
HandleInput ();
OnGround = IsGrounded ();
HandleMovement ();
Flip (direction);
HandleLayers ();
MovePlayer (direction);
}
}
void Update()
{
StartCoroutine (checkifDead ());
}
public void OnDead()
{
if(Dead != null)
{
Dead ();
}
}
private void HandleMovement()
{
if (MyRigidbody.velocity.y < 0)
{
MyAnimator.SetBool ("land", true);
}
if (Jump && MyRigidbody.velocity.y == 0)
{
MyRigidbody.AddForce (new Vector2 (0, jumpForce));
}
}
private void Flip(float direction){
if (direction > 0 && !facingRight || direction < 0 && facingRight) {
playerChangeDirection ();
}
}
private void HandleInput()
{
if (Input.GetKeyDown (KeyCode.Z))
{
MyAnimator.SetTrigger ("attack");
}
if (Input.GetKeyDown (KeyCode.Space))
{
MyAnimator.SetTrigger ("jump");
}
}
private bool IsGrounded()
{
if (MyRigidbody.velocity.y <= 0) {
foreach (Transform point in groundPoints) {
Collider2D[] colliders = Physics2D.OverlapCircleAll (point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++) {
if (colliders [i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!OnGround)
{
MyAnimator.SetLayerWeight (1, 1);
} else
{
MyAnimator.SetLayerWeight (1, 0);
}
}
public void mobileAttack()
{
MyAnimator.SetTrigger ("attack");
}
public void mobileJump()
{
MyAnimator.SetTrigger ("jump");
}
public void mobileMoveleft()
{
direction = -movementSpeed;
}
public void mobileMoveright()
{
direction = movementSpeed;
}
public void StopMoving()
{
direction = 0;
}
void MovePlayer(float playerSpeed)
{
if (!Attack && (OnGround || airControl))
{
MyRigidbody.velocity = new Vector2 (direction, MyRigidbody.velocity.y);
}
MyAnimator.SetFloat ("speed", Mathf.Abs(direction));
}
public override IEnumerator TakeDamage ()
{
health -= enemy.damage;
if (!isDead) {
MyAnimator.SetTrigger ("damage");
}
else
{
MyAnimator.SetLayerWeight (1, 0);
MyAnimator.SetTrigger ("die");
}
yield return null;
}
public override bool isDead
{
get
{
if (health <= 0)
{
playerdeadfail = true;
OnDead ();
}
return health <= 0;
}
}
public IEnumerator checkifDead()
{
if (playerdeadfail && !successfail.failsource.isPlaying)
{
// playerdeadfail = true;
successfail.fail = true;
menucontroller.StartCoroutine ("FadeSound");
successfail.missionFail ();
yield return null;
playerdeadfail = false;
}
}
}
using UnityEngine; using System.Collections; using System.Collections.Generic;
public abstract class Character : MonoBehaviour {
public gamestarting starting;
[SerializeField]
protected int health;
[SerializeField]
private EdgeCollider2D swordCollider;
public EdgeCollider2D SwordCollider
{
get
{
return swordCollider;
}
}
[SerializeField]
private List <string> damageSources;
public bool TakingDamage { get; set; }
public abstract bool isDead { get; }
public float direction;
public Animator MyAnimator { get; private set; }
[SerializeField]
protected float movementSpeed;
[SerializeField]
public int damage;
protected bool facingRight;
[SerializeField]
protected GameObject knifePrefab;
public bool Attack { get; set; }
// Use this for initialization
public virtual void Start () {
starting = GameObject.FindObjectOfType<gamestarting> ();
facingRight = true;
MyAnimator = GetComponent<Animator> ();
}
void Update () {
}
public abstract IEnumerator TakeDamage ();
public void playerChangeDirection()
{
facingRight = !facingRight;
transform.localScale = new Vector3 (transform.localScale.x * -1, 0.37f, 1);
}
public void ChangeDirection()
{
facingRight = !facingRight;
transform.localScale = new Vector3 (transform.localScale.x * -1, 0.5f, 1);
}
public void ThrowKnife(int value)
{
if (!facingRight)
{
GameObject tmp = (GameObject)Instantiate (knifePrefab, transform.position, Quaternion.Euler(new Vector3(0,0,-90)));
tmp.GetComponent<Knife> ().Initialize (Vector2.right);
}
else
{
GameObject tmp = (GameObject)Instantiate (knifePrefab, transform.position, Quaternion.Euler(new Vector3(0,0,90)));
tmp.GetComponent<Knife> ().Initialize (Vector2.left);
}
}
public void MeleeAttack()
{
SwordCollider.enabled = true;
}
public virtual void OnTriggerEnter2D(Collider2D other)
{
if (damageSources.Contains(other.tag))
{
StartCoroutine (TakeDamage());
}
}
}
using UnityEngine; using System.Collections; using UnityEngine.UI; using System;
public class TimeManager : MonoBehaviour {
public float startingTime;
private Text theText;
private MenuCtrl menucontroller;
private gamestarting starting;
private SuccessFail successfail;
// Use this for initialization
void Start () {
starting = GameObject.FindObjectOfType<gamestarting> ();
menucontroller = GameObject.FindObjectOfType<MenuCtrl> ();
successfail = GameObject.FindObjectOfType<SuccessFail> ();
theText = GetComponent<Text> ();
}
void FixedUpdate () {
if (starting.countdownDone) // if game starting canvas done
{
startingTime -= Time.deltaTime;
theText.text = "" + Math.Round (startingTime, 2);
if (startingTime <= 0 && !successfail.failsource.isPlaying) // if timer reach 0
{
//dead animation//
successfail.fail = true;
menucontroller.StartCoroutine ("FadeSound");
successfail.missionFail ();
}
}
}
}