Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Shen_Racc · Aug 10, 2021 at 10:21 AM · listqueue

Im not sure if i should use a list or queue for a battle sequence

I created a game for an assignment in school, however, I have trouble trying to move on to the next enemy after the first one is killed. I have created prefabs of each enemy. My end goal is after defeating the enemy, it will destroy the prefab and instantiate the next one until all enemies are defeated.

public enum BattleTurn { STARTFIGHT, PLAYERTURN, ENEMYTURN, NEXTENEMY, GAMEWON, GAMELOST } public class Combat : MonoBehaviour { public BattleTurn turn;

 public GameObject Player;
 public GameObject Enemy1;
 /*public GameObject Enemy2;
 public GameObject Enemy3;
 public GameObject Enemy4;
 public GameObject Enemy5;
 public GameObject Enemy6;
 public GameObject Enemy7;*/

 Character playerUnit;
 Character enemyUnit;

 public Transform PlayerStats;
 public Transform EnemyStats;

 public Text Dialogue;

 public GameHUD playerHUD;
 public GameHUD enemyHUD;

 // Start is called before the first frame update
 void Start() //When the game starts it sets the state to the StartFight
 {
     turn = BattleTurn.STARTFIGHT;
     Debug.Log("Game Starts");

     StartCoroutine(StartBattle());
 }

 IEnumerator StartBattle() //When the game is ran, it creates a player and enemy
 {
     GameObject playerUI = Instantiate(Player, PlayerStats);
     playerUnit = playerUI.GetComponent<Character>();

     GameObject enemyUI = Instantiate(Enemy1, EnemyStats);
     enemyUnit = enemyUI.GetComponent<Character>();
     /*
     GameObject enemyUI = Instantiate([enemy in list/queue], EnemyStats);
     enemyUnit = enemyUI.GetComponent<Character>();
     */

     Dialogue.text = "A " + enemyUnit.characterName + " Appears";

     playerHUD.SetHUD(playerUnit);
     enemyHUD.SetHUD(enemyUnit);

     yield return new WaitForSeconds(2f);
     //after creating the characters, it sets to the player's turn
     turn = BattleTurn.PLAYERTURN;
     StartPlayersTurn();
 }

 void StartPlayersTurn() // This sets the text to your move so the player can attack
 {
     Dialogue.text = " Your Move ";
 }

 public void OnAttackButton() //When the attack button is clicked, it starts the playerattack function
 {
     if (turn != BattleTurn.PLAYERTURN)
         return;

     StartCoroutine(PlayerAttack());
 }

 IEnumerator PlayerAttack() //When the player uses the attack button it will do damage based on the players damage to the enemy
 {
     bool isDead = enemyUnit.TakeDamage(playerUnit.damage);

     enemyHUD.SetHP(enemyUnit.CurrentHp);
     Dialogue.text = "You did " + playerUnit.damage;
     yield return new WaitForSeconds(2f);
     //this checks if the enemy is dead, and changes the states based on it
     if (isDead)
     {
         turn = BattleTurn.NEXTENEMY;
         EndBattle();
     }
     else
     {
         turn = BattleTurn.ENEMYTURN;
         StartCoroutine(EnemyTurn());
     }
     /*
     else(if all enemies are defeated){
         turn=BattleTurn.GAMEWON
     } 
     */
 }

 IEnumerator EnemyTurn()
 {
     Dialogue.text = enemyUnit.characterName + "Attacks You";

     yield return new WaitForSeconds(1f);

     bool isDead = playerUnit.TakeDamage(enemyUnit.damage);
     playerHUD.SetHP(playerUnit.CurrentHp);

     yield return new WaitForSeconds(1f);

     if (isDead)
     {
         turn = BattleTurn.GAMELOST;
         EndBattle();
     }else
     {
         turn = BattleTurn.PLAYERTURN;
         StartPlayersTurn();
     }
 }

 void EndBattle()
 {
     if(turn == BattleTurn.NEXTENEMY)
     {
         Dialogue.text = "You Move On To The Next Enemy";
         StartCoroutine(NextFight());
     }
     else if(turn == BattleTurn.GAMEWON)
     {
         Dialogue.text = "You Have Won";
     }else if(turn == BattleTurn.GAMELOST)
     {
         Dialogue.text = "You Have Lost";
     }
 }
 IEnumerator NextFight()
 {
     Dialogue.text = "Another enemy enters";
     //Destroy(monster);
     //Get next monster in list/queue
     yield return new WaitForSeconds(2f);
 }

}

Comment
Add comment · Show 1
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 logicandchaos · Aug 10, 2021 at 04:38 PM 0
Share

you shouldn't destroy and instantiate, look into pooling.

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

126 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

C# - Is it possible to sync a Queue to a List, or is there a better option? 1 Answer

Creating List of Player 1 Answer

Strange behavior with map connectivity 1 Answer

public Queue 2 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