Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by meadseu · Oct 16, 2016 at 05:22 PM · unity 5coroutineupdate function

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 ();
         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Listening keypresses out of Update? 1 Answer

Testing Duration With Unit Tests 0 Answers

display rotating animation while calculating operation that takes long time 0 Answers

Coroutine [sometimes] stops working 1 Answer

Trying to move buttons via Coroutine and transform.Translate 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges