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 TiagoCarvalho · May 14, 2013 at 10:34 AM · yield waitforseconds

yield return new wait for seconds c# Problem

Hey, i have a problem with my script, when the bool iAmDestroyed is set to true, than it starts a count down to respawn the player into the game, and when the countdown beguins i want to draw in the screen "Wait 5 seconds to respawn" but the problem is that when the player respawns it instantiates more and more players in the game and dont stop. If i set iAmDestroyed to false, than the message "Wait 5 seconds to respawn" dont appears but the player spawns normally.. Can someone help me with my script?

using UnityEngine; using System.Collections;

public class SpawnScript : MonoBehaviour {

 //Variables Start___________________________________
 
 //Used to determine if the palyer needs to spawn into
 //the game.
 
 private bool justConnectedToServer = false;
 
 
 //Used to determine which team the player is on.
 
 public bool amIOnTheRedTeam = false;
 
 public bool amIOnTheBlueTeam = false;
 
 
 //Used to define the JoinTeamWindow.
 
 private Rect joinTeamRect;
 
 private Rect respawnRect;
 
 private string joinTeamWindowTitle = "Team Selection";
 
 private string respawnWindowTitle = "Wait for respawn";
 
 private string respawn = "Wait for respawn";
 
 private int joinTeamWindowWidth = 330;
 
 private int joinTeamWindowHeight = 100;
 
 private int joinTeamLeftIndent;
 
 private int joinTeamTopIndent;
 
 private int respawnLeftIndent;
 
 private int respawnTopIndent;
 
 private int buttonHeight = 40;
 
 public string playerName;
 
 private GameObject multiplayerManager;
 
 private MultiplayerScript multiScript;
 
 public int waitTime = 5;
 
 
 //The Player prefabs are connected to these in the 
 //inspector
 
 public Transform redTeamPlayer;
 
 public Transform blueTeamPlayer;
 
 private int redTeamGroup = 0;
 
 private int blueTeamGroup = 1;
 
 
 //Used to capture spawn points.
 
 private GameObject[] redSpawnPoints;
 
 private GameObject[] blueSpawnPoints;
 
 
 //Used in determining whether the player is destroyed.
 
 public bool iAmDestroyed = false;
 
 
 //Used in determining if the player has spawned for the
 //first time.
 
 public bool firstSpawn = false;
 
 
 //This is used in allowing the player to select a team again
 //if the match has restarted.
 
 public bool matchRestart = false;

 //Variables End_____________________________________
 
 // Use this for initialization
 void Start () {
     

// PlayerDatabase dataScript = gameManager.GetComponent();

 }
 
 // Update is called once per frame
 void Update () {
     
 }
 
 
 void OnConnectedToServer ()
 {
     justConnectedToServer = true;    
 }
 
 
 
 void JoinTeamWindow (int windowID)
 {    
     //Only show these two buttons when the player has just connected to
     //the server or if the match has restarted. They allow the player
     //choose a team and spawn into the game.
     
     
     if(justConnectedToServer == true || matchRestart == true)
     {
         //If the player clicks on the Join Red Team button then
         //assign them to the red team and spawn them into the game.
         
         if(GUILayout.Button("Join Red Team", GUILayout.Height(buttonHeight)))
         {
             amIOnTheRedTeam = true;
             
             justConnectedToServer = false;
             
             matchRestart = false;
             
             iAmDestroyed = false;
             
             SpawnRedTeamPlayer();
             
             firstSpawn = true;
         }
         
         
         //If the player clicks on the Join Blue Team button then
         //assign them to the blue team and spawn them into the game.
         
         if(GUILayout.Button("Join Blue Team", GUILayout.Height(buttonHeight)))
         {
             amIOnTheBlueTeam = true;
             
             justConnectedToServer = false;
             
             matchRestart = false;
             
             iAmDestroyed = false;
             
             SpawnBlueTeamPlayer();
             
             firstSpawn = true;
         }
     }
     
 }
 
 void respawnWindow (int windowID)
 {
     if(iAmDestroyed == true && amIOnTheBlueTeam == true)
     {
     StartCoroutine(RespawnBlue());
     }
     
     if(iAmDestroyed == true && amIOnTheRedTeam == true)
     {    
     StartCoroutine(RespawnRed());
     }
     
 }


 
 void OnGUI()
 {
     //If the player has just connected to the server then draw the 
     //Join Team window.
     
     if(justConnectedToServer == true || matchRestart == true
        && Network.isClient)
     {    
     
         Screen.lockCursor = false;
         
         joinTeamLeftIndent = Screen.width / 2 - joinTeamWindowWidth / 2;
         
         joinTeamTopIndent = Screen.height / 2 - joinTeamWindowHeight / 2;
         
         joinTeamRect = new Rect(joinTeamLeftIndent, joinTeamTopIndent,
                                 joinTeamWindowWidth, joinTeamWindowHeight);
         
         joinTeamRect = GUILayout.Window(0, joinTeamRect, JoinTeamWindow,
                                         joinTeamWindowTitle);
                                         
     }
     
     if(iAmDestroyed == true)
     {
         joinTeamLeftIndent = Screen.width / 2 - joinTeamWindowWidth / 2;
         
         joinTeamTopIndent = Screen.height / 2 - joinTeamWindowHeight / 2;
         
         joinTeamRect = new Rect(joinTeamLeftIndent, joinTeamTopIndent,
                                 joinTeamWindowWidth, joinTeamWindowHeight);
         
         joinTeamRect = GUILayout.Window(0, joinTeamRect, respawnWindow,
                                         joinTeamWindowTitle);
         
         GUILayout.Box("Wait for respawn");
     }
     
     
 }
 
 
 void SpawnRedTeamPlayer ()
 {
     //Find all red spawn points and place a reference to them in the array
     //redSpawnPoints.
     
     iAmDestroyed = false;
     
     redSpawnPoints = GameObject.FindGameObjectsWithTag("SpawnRedTeam");
     
     
     //Randomly select one of those spawn points.
     
     GameObject randomRedSpawn = redSpawnPoints[Random.Range(0, redSpawnPoints.Length)];
     
     
     //Instantiate the player at the randomly selected spawn point.
     
     Network.Instantiate(redTeamPlayer, randomRedSpawn.transform.position,
                         randomRedSpawn.transform.rotation, redTeamGroup);
     
     if(firstSpawn != true)
         {
         playerName = "PlayerRed";
         }
     
     //Access the PlayerDatabase and supply it with the team that this player
     //has joined.
     
     GameObject gameManager = GameObject.Find("GameManager");
     
     PlayerDatabase dataScript = gameManager.GetComponent();
     
     dataScript.joinedTeam = true;
     
     dataScript.playerTeam = "red";
     
     iAmDestroyed = false;
 }
 
 
 
 void SpawnBlueTeamPlayer ()
 {
     //Find all blue spawn points and place a reference to them in the array
     //blueSpawnPoints.
     
     
     blueSpawnPoints = GameObject.FindGameObjectsWithTag("SpawnBlueTeam");
     
     
     //Randomly select one of those spawn points.
     
     GameObject randomBlueSpawn = blueSpawnPoints[Random.Range(0, blueSpawnPoints.Length)];
     
     
     //Instantiate the player at the randomly selected spawn point.
     
     Network.Instantiate(blueTeamPlayer, randomBlueSpawn.transform.position,
                         randomBlueSpawn.transform.rotation, blueTeamGroup);
     
     if(firstSpawn != true)
         {
         playerName = "PlayerBlue";
         }
     
     //Access the PlayerDatabase and supply it with the team that this player
     //has joined.
     
     GameObject gameManager = GameObject.Find("GameManager");
     
     PlayerDatabase dataScript = gameManager.GetComponent();
     
     dataScript.joinedTeam = true;
     
     dataScript.playerTeam = "blue";
     
     iAmDestroyed = false;
 }
 
 
 
 IEnumerator RespawnBlue ()
 {
     yield return new WaitForSeconds (waitTime);
     
     iAmDestroyed = false;
     SpawnBlueTeamPlayer();
 }
 
 IEnumerator RespawnRed ()
 {
     yield return new WaitForSeconds (waitTime);
     
     iAmDestroyed = false;
     SpawnRedTeamPlayer();    
     
 }
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

}

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 Mexallon · May 14, 2013 at 10:45 AM 1
Share

I strongly recommend you to seperate scripts. One script (one class) should have one task. If you follow that guide your life as a programmer ist much easier. The title of the script is spawn script though it seems to handle the gui too.

avatar image ExTheSea · May 14, 2013 at 06:03 PM 0
Share

Your problem is that until the 5 seconds are over you're starting the Coroutine everytime OnGUI is called which is pretty much every frame. I would recommend you too to split the GUI stuff and the respawn part.

When the Player dies you start the Respawn-Coroutine and maybe start another coroutine which just increases a number every second until it gets to 5. In OnGUI then you can just start displaying the number if the number is between 1 and 5 or you use a boolean which isn't involved in the actual respawning.

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

15 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

Related Questions

Problem with yield WaitForSeconds (I think) -javascript) 1 Answer

problem with terrain collider 0 Answers

In Game Animation 2 Answers

Do not delete GUI after 2nd collision 1 Answer

SmoothMoves C# Animation Problem 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