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 /
avatar image
0
Question by m0mohy92 · Jul 30, 2019 at 01:49 PM · coroutineshootingenemy spawnenemydamage

How to make two enemies appear in sequence?

The game is shooting game, i need the second enemy appear only when the first one is hit, and the first one reappears again after the second one is hit. And every one stays for 3 seconds, i know it seems a coroutine issue, but i can not handle it, any help please?

Comment
Add comment · Show 2
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
avatar image I_Am_Err00r · Jul 30, 2019 at 02:44 PM 0
Share

Please provide code of what you are currently doing.


I'm also a little confused, let me see if I understand:

  1. You want enemy1 to spawn

  2. You want enemy2 to spawn only after enemy1 is hit.

  3. You want enemy1 to respawn if enemy2 is hit.

  4. This is where I'm confused: You want everything to appear for 3 seconds? Does this override if someone was hit? What do you mean "And every one stays for 3 seconds"? This statement seems to contradict the previous three I listed above the way I'm understanding this right now.

avatar image m0mohy92 I_Am_Err00r · Jul 30, 2019 at 02:56 PM 0
Share

@I_Am_Err00r I mean if the enemy is hit before 3 seconds it will disappear, if not hit it will wait till 3 seconds then disappear and the other enemy appears.

Here is the code:

public class Target : $$anonymous$$onoBehaviour { public int position;

 public GameObject activeRobot;

 [SerializeField]
 private GameObject[] robots;
 [SerializeField]

  private void OnCollisionEnter(Collision collision)
      {
          Destroy(collision.collider.gameObject);
          activeRobot.GetComponent<Animator>().Play("Die");
          game.AddHit();
          GetComponent<BoxCollider>().enabled = false;
          activeRobot = null;
          StartCoroutine("AliveTimer");
          StartCoroutine("DeathTimer");
      }
  
      public void ActivateRobot()
      {
          activeRobot = robots[Random.Range(0,3)];
          activeRobot.SetActive(true);
          activeRobot.GetComponent<Animator>().Play("Rise");
          GetComponent<BoxCollider>().enabled = true;
      }
  
      public void ActivateRobot(RobotTypes type)
      {
          StopAllCoroutines();
          activeRobot = robots[(int)type];
          activeRobot.SetActive(true);
          activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
          GetComponent<BoxCollider>().enabled = true;
      }
  
      IEnumerator AliveTimer()
      {
          yield return new WaitForSeconds (3);
          ActivateRobot();
      }
  
      IEnumerator DeathTimer()
      {
          yield return new WaitForSeconds(Random.Range(10, 14));
          if (activeRobot == null)
          {
              yield break;
          }
          activeRobot.GetComponent<Animator>().Play("Die");
          GetComponent<BoxCollider>().enabled = false;
          activeRobot = null;
          StartCoroutine("AliveTimer");
      }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by m0mohy92 · Jul 30, 2019 at 02:55 PM

@I_Am_Err00r I mean if the enemy is hit before 3 seconds it will disappear, if not hit it will wait till 3 seconds then disappear and the other enemy appears.

Here is the code:

 private void OnCollisionEnter(Collision collision)
     {
         Destroy(collision.collider.gameObject);
         activeRobot.GetComponent<Animator>().Play("Die");
         game.AddHit();
         GetComponent<BoxCollider>().enabled = false;
         activeRobot = null;
         StartCoroutine("AliveTimer");
         StartCoroutine("DeathTimer");
     }
 
     public void ActivateRobot()
     {
         activeRobot = robots[Random.Range(0,3)];
         activeRobot.SetActive(true);
         activeRobot.GetComponent<Animator>().Play("Rise");
         GetComponent<BoxCollider>().enabled = true;
     }
 
     public void ActivateRobot(RobotTypes type)
     {
         StopAllCoroutines();
         activeRobot = robots[(int)type];
         activeRobot.SetActive(true);
         activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
         GetComponent<BoxCollider>().enabled = true;
     }
 
     IEnumerator AliveTimer()
     {
         yield return new WaitForSeconds (3);
         ActivateRobot();
     }
 
     IEnumerator DeathTimer()
     {
         yield return new WaitForSeconds(Random.Range(10, 14));
         if (activeRobot == null)
         {
             yield break;
         }
         activeRobot.GetComponent<Animator>().Play("Die");
         GetComponent<BoxCollider>().enabled = false;
         activeRobot = null;
         StartCoroutine("AliveTimer");
     }
Comment
Add comment · Show 4 · Share
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
avatar image I_Am_Err00r · Jul 30, 2019 at 03:36 PM 0
Share

Is your code compiling? I see you have two methods of the same name:

  public void ActivateRobot()
      {
          activeRobot = robots[Random.Range(0,3)];
          activeRobot.SetActive(true);
          activeRobot.GetComponent<Animator>().Play("Rise");
          GetComponent<BoxCollider>().enabled = true;
      }
  
      public void ActivateRobot(RobotTypes type)
      {
          StopAllCoroutines();
          activeRobot = robots[(int)type];
          activeRobot.SetActive(true);
          activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
          GetComponent<BoxCollider>().enabled = true;
      }
avatar image m0mohy92 I_Am_Err00r · Jul 30, 2019 at 03:45 PM 0
Share

Yes it is.

avatar image I_Am_Err00r m0mohy92 · Jul 30, 2019 at 04:02 PM 0
Share

Turns out you're right, as long as return type is different it will work, I guess I've never named a method the exact same with a different return type; that can be very confusing for anyone besides you and anyone else who knows the code backwards and forwards, I would consider changing one of them to avoid that confusion in the future; regardless, I never see:

 public void ActivateRobot(RobotTypes type)
       {
           StopAllCoroutines();
           activeRobot = robots[(int)type];
           activeRobot.SetActive(true);
           activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
           GetComponent<BoxCollider>().enabled = true;
       }

getting called within this script, is that getting called in another?


However, I would probably go about all this like this:

     float time;
     
     //can be public or private
     public void Update()
     {
     time += Time.deltaTime()
     if(time > 3f)
          {
            ActivateRobot();
           }
     } 
     
     private void OnCollisionEnter(Collision collision)
          {
              Destroy(collision.collider.gameObject);
              activeRobot.GetComponent<Animator>().Play("Die");
              game.AddHit();
              GetComponent<BoxCollider>().enabled = false;
              activeRobot = null;
              ActivateRobot();
              StartCoroutine("DeathTimer");
          }
     
      
          public void ActivateRobot()
          {
              activeRobot = robots[Random.Range(0,3)];
              activeRobot.SetActive(true);
              activeRobot.GetComponent<Animator>().Play("Rise");
              GetComponent<BoxCollider>().enabled = true;
              time = 0; //I don't know if you want that 3 second timer to reset every time, but if you don't just add this before time = 0: if(time <  3) { return;} or something like that.
          }
      
          public void ActivateRobot(RobotTypes type)
          {
              StopAllCoroutines();
              activeRobot = robots[(int)type];
              activeRobot.SetActive(true);
              activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
              GetComponent<BoxCollider>().enabled = true;
          }
      
          IEnumerator AliveTimer()
          {
              yield return new WaitForSeconds (3);
              ActivateRobot();
          }
      
          IEnumerator DeathTimer()
          {
              yield return new WaitForSeconds(Random.Range(10, 14));
              if (activeRobot == null)
              {
                  yield break;
              }
              activeRobot.GetComponent<Animator>().Play("Die");
              GetComponent<BoxCollider>().enabled = false;
              activeRobot = null;
              StartCoroutine("AliveTimer");
          }
 

Show more comments

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

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making Coroutine Repeat? 3 Answers

WaitForSeconds behaves weirdly 1 Answer

Enemy Damage problem 1 Answer

Please, I need help, I'm trying to create a Coroutine where my enemy shoots 1 pack of 4 bullets, every 2 seconds, but it just ended up in the enemy shooting some bullets and then never again 1 Answer

Char shots oneside only / kills enemy on specific place. 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