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
0
Question by Symbie · Jun 04, 2020 at 01:25 AM · scene-switchingdeletelives

Deleting life object from scene

Hi, im creating a game where to players spawn in and run around over little pads, giving them points. there is a timer going and when the timer runs out the game stops and the player with the most points win, at the moment once the time runs out a new scene is loaded and says which player won and the score they had, then it goes back to the game scene and starts again. i added 3 objects in the top right of the screen and 3 in the top left, these represent lives. once the round ends, i want the losing player to lose a life, i cannot get this working however. at the moment i only have it coded that if the playerOne loses, then they lose a life, this works for the first life, but not the second or third. these are the relevant scrips, how should i fix this issue?

Player Lives

 using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
 using UnityEngine;
 
 public class PlayerLives : MonoBehaviour
 {
 
     public static bool playerTwoRound1 = false;
     public static bool playerTwoRound2 = false;
     public static bool playerTwoRound3 = false;
 
     public GameObject playerOneLifeOneObj;
     public GameObject playerOneLifeTwoObj;
     public GameObject playerOneLifeThreeObj;
     void Update()
     {
       
     if(Coutndown.playerTwoWins)
         {
             playerTwoRound1 = true;
             Coutndown.playerTwoWins = false;
         }
     if(Coutndown.playerTwoWins && playerTwoRound1)
         {
             playerTwoRound2 = true;
             Coutndown.playerTwoWins = false;
         }
     if(Coutndown.playerTwoWins && playerTwoRound3)
         {
             playerTwoRound3 = true;
             Coutndown.playerTwoWins = false;
         }
 
 
     if(playerTwoRound1)
         {
             Destroy(playerOneLifeOneObj);
         }
     if(playerTwoRound2)
         {
             Destroy(playerOneLifeTwoObj);
         }
         if (playerTwoRound3)
         {
             Destroy(playerOneLifeThreeObj);
         }
 
     }
 }
 

This script is what gets the life objects and is suppose to delete them, the other script is the Countdown Script

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Coutndown : MonoBehaviour
 {
     float timeLeft = 3.0f;
     float gameTimeLeft = 15.0f;
     float waitForWin = 2.0f;
 
     public Text text;
     public Text gameTime;
     public Text playerOneScore;
     public Text playerTwoScore;
 
     public GameObject spawnObject;
     PlayerSpawn spawnScript;
 
     public GameObject playerOne;
     PlayerController playerOneScript;
     Knockback playerOneKnockback;
 
     public GameObject playerTwo;
     PlayerController playerTwoScript;
     Knockback playerTwoKnockback;
 
     public GameObject gameManager;
     Score scoreScript;
 
     public bool startGame;
     public bool endGame;
 
     public static bool playerTwoWins = false;
 
     void Start()
     {
         spawnScript = spawnObject.GetComponent<PlayerSpawn>();
         playerOneScript = playerOne.GetComponent<PlayerController>();
         playerTwoScript = playerTwo.GetComponent<PlayerController>();
         playerOneKnockback = playerOne.GetComponent<Knockback>();
         playerTwoKnockback = playerTwo.GetComponent<Knockback>();
         scoreScript = gameManager.GetComponent<Score>();
 
         startGame = false;
         endGame = false;
     }
 
     void Update()
     {
         if (spawnScript.playersSpawned)
         {
             StartCountdown();
         }
         
     }
 
     void StartCountdown()
     {
         timeLeft -= Time.deltaTime;
         text.text = Mathf.Round(timeLeft).ToString();
         if (timeLeft < 1)
         {
             text.text = "GO!";
         }
         if (timeLeft < 0)
         {
             playerOneScript.speed = 10f;
             playerTwoScript.speed = 10f;
             playerOneKnockback.started = true;
             playerTwoKnockback.started = true;
             text.text = "";
 
             StartGame();
 
         }
     }
 
     void StartGame()
     {
         gameTimeLeft -= Time.deltaTime;
         gameTime.text = Mathf.Round(gameTimeLeft).ToString();
         if(gameTimeLeft < 0)
         {
             EndGame();
         }
     }
 
     void EndGame()
     {
         playerOneScript.speed = 0f;
         playerTwoScript.speed = 0f;
 
         gameTime.text = "";
         playerOneScore.text = "";
         playerTwoScore.text = "";
 
         text.text = "Times Up!";
         text.fontSize = 150;
 
         if (scoreScript.playerOneScore > scoreScript.playerTwoScore)
         {
             waitForWin -= Time.deltaTime;
 
             if (waitForWin < 0)
             {
                 SceneManager.LoadScene("PlayerOneWin");
             }
 
         }
         else if (scoreScript.playerTwoScore > scoreScript.playerOneScore)
         {
             waitForWin -= Time.deltaTime;
 
             if (waitForWin < 0)
             {
                 playerTwoWins = true;
                 SceneManager.LoadScene("PlayerTwoWin");
             }
         }
         else if (scoreScript.playerOneScore == scoreScript.playerTwoScore)
         {
             waitForWin -= Time.deltaTime;
 
             if (waitForWin < 0)
             {
                 SceneManager.LoadScene("Draw");
             }
         }
 
 
     }
 
 
 
 }

This script sets timers throughout the game, like once the players spawn in, a 3 second countdown starts, then another timer starts which is how long the game goes for. in the EndGame function i say if the playerTwo score is higher than the player two wins, and the playerTwoWins bool is set to true, which is what im using to delete the objects in the first script.

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

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

Related Questions

Load previous scene 1 Answer

Change Scene by Drag 0 Answers

"Loading scene" (progress bar) that should not destroy until my own stuff ended 1 Answer

Make a scene continue running in background 1 Answer

UI canvas or panel that only appears once per game session? 1 Answer


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