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 Wizardman290 · Nov 18, 2014 at 09:05 PM · c#errorrespawndeath

How to fix my code? (Player death + Respawn)

Again, this was declined by the moderators overnight, so if this is against the rules, sorry, but I don't see why this would be rejected. (Last time it happened it was overnight as well)

Okay, so I made this code so that after 2 seconds some stuff gets turned off, (Player death) and then the position is reset, and that stuff gets turned back on after five seconds, but i get and error saying

Assets/Scripts/AttributeDeclaration.cs(131,14): error CS1624: The body of AttributeDeclaration.KillPlayer()' cannot be an iterator block because bool' is not an iterator interface type

I am new to programming/coding, and have no clue what this means.

Here is my code that I'm making (I have a lot going on in this, and a part of it has absolutley nothing to do with respawning, sorry. I wanted to get all of the player essentials into this one code.)

Here is the code it is all in.

 using UnityEngine;
 using System.Collections;
 
 public class AttributeDeclaration : MonoBehaviour {
     #region Public ints
     public int Health = 100;
     public int Magic = 100;
     public int Strength = 100;
     public int Stamina = 100;
     public int Defense = 100;
 
     //Maximum attributes
     public int MaxHealth = 100;
     public int MaxMagic = 100;
     public int MaxStrength = 100;
     public int MaxStamina = 100;
     public int MaxDefense = 100;
 
     //Testing Stuff
     public int DamageAmount = 20;
 
     //---------------------------------------------------------------------------------------------
     #endregion
 
     public PlayerLevelDeclaration PT;
     public PlayerMovement PM;
     public HealthPotion HP;
 
     //----------------------------------------------------------------------------------------
     void Awake(){
         newPos = transform.position;
     }
     //-------------------------
     // Use this for initialization
     void Start () {
     }
     //-----------------------------------------------------------------------------------------
     // Update is called once per frame
     void Update () {
         PT = gameObject.GetComponent<PlayerLevelDeclaration>();
         HP = gameObject.GetComponent<HealthPotion>();
         PM = gameObject.GetComponent<PlayerMovement>();
 
         TempDamageHealth();
         TempDamageStamina();
         TempDamageMagic();
         TempDamageStrength();
         TempDamageDefense();
 
         CheckStats();
 
         KillPlayer();
     }
     //----------------------------------------------------------------------------------------
     void CheckStats(){
         if(Health <= 0)
         {
             Health = 0;
         }
         if(Stamina <= 0)
         {
             Stamina = 0;
         }
         if(Magic <= 0)
         {
             Magic = 0;
         }
         if(Strength <= 0)
         {
             Strength = 0;
         }
         if(Defense <=0)
         {
             Defense = 0;
         }
     }
     //-----------------------------------------------------------------------------------------
 
     public int TempDamageHealth()
     {
 
         if(Input.GetKeyDown(KeyCode.Keypad1))
         {
             Health = Health - DamageAmount;
         }
         return Health;
     }
     //-------------------------------------------------------------------------------------
     public int TempDamageStamina()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad2))
         {
             Stamina = Stamina - DamageAmount;
         }
         return     Stamina;
     }
     //--------------------------------------------------------------------------------------
     public int TempDamageMagic()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad3))
         {
             Magic = Magic - DamageAmount;
         }
         return Magic;
     }
     //------------------------------------------------------------------------------------------
     public int TempDamageStrength()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad4))
         {
             Strength = Strength - DamageAmount;
         }
         return Strength;
     }
     //----------------------------------------------------------------------------------
     public int TempDamageDefense()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad9))
         {
             Defense = Defense - DamageAmount;
         }
         return Defense;
     }
 //--------------------------------------------------------------------
     Vector3 newPos;
     bool IsAlive = false;
     bool KillPlayer(){
     if(Health <= 0)
         {
             IsAlive = false;
             Health = 0;
             //Hopefully this makes it so the player can't move as soon as Health = 0, but doesn't disappear and lose 
             //collision until after 2 seconds. Then, 5 seconds later, the player's position is set to 0, 0, 0, and 
             //He regains movement, reappears, and regains collision.
 
             //Kill Player
             PM.enabled = false;
             yield return new WaitForSeconds(2f);
             renderer.enabled = false;
             collider.enabled = false;
             PM.enabled = false;
                     
             //Wait 5 seconds
             yield return new WaitForSeconds(5f);
 
             //Set position code;
             Vector3 PlayerSpawn = new Vector3(0f , 0f , 0f);
             newPos = PlayerSpawn;
             transform.position = newPos;
             //Rest
             renderer.enabled = true;
             collider.enabled = true;
             PM.enabled = true;
             IsAlive = true;
         }
     }
 }
 

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by atr0phy · Nov 18, 2014 at 11:57 PM

Is there a particular reason you are using yield return as opposed to simply return? A method using yield return must be declared as returning one of the following two interfaces:

IEnumerable or IEnumerator

Example:

 public IEnumerable YourMethod() {
     foreach (XElement header in headersXml.Root.Elements()) {
         yield return (ParseHeader(header));                
     }
 }

So there is a difference between return and yield return, and you need to declare the method correctly.

Comment
Add comment · Show 2 · 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 Wizardman290 · Nov 18, 2014 at 11:59 PM 0
Share

I don't know exactly, I was talking to someone who said I need to use yields. I essentialy want to constantly check if playerhealth <= 0, and if it is, make player movement stop, and the $$anonymous$$esh and BoxCollider disable, then after 5 seconds, all of them go back on.

avatar image Wizardman290 · Nov 19, 2014 at 12:06 AM 0
Share

When I change my code to this, the error goes away, but nothing happens when the health is 0...

 Vector3 newPos;
     bool IsAlive = false;
     IEnumerable $$anonymous$$illPlayer(){
     if(Health <= 0)
         {
             IsAlive = false;
             Health = 0;
             //Hopefully this makes it so the player can't move as soon as Health = 0, but doesn't disappear and lose 
             //collision until after 2 seconds. Then, 5 seconds later, the player's position is set to 0, 0, 0, and 
             //He regains movement, reappears, and regains collision.
 
             //$$anonymous$$ill Player
             P$$anonymous$$.enabled = false;
             yield return new WaitForSeconds(2f);
             renderer.enabled = false;
             collider.enabled = false;
             P$$anonymous$$.enabled = false;
                     
             //Wait 5 seconds
             yield return new WaitForSeconds(5f);
 
             //Set position code;
             Vector3 PlayerSpawn = new Vector3(0f , 0f , 0f);
             newPos = PlayerSpawn;
             transform.position = newPos;
             //Rest
             renderer.enabled = true;
             collider.enabled = true;
             P$$anonymous$$.enabled = true;
             IsAlive = true;
 
 
         }
         yield return IsAlive;
     }
avatar image
0

Answer by AngryBurritoCoder · Nov 18, 2014 at 10:50 PM

When you code, and an error pops up, next time check the lines the error is at in your case its at (131,14), AND you can see at that line that you are declaring the method public void KillPlayer() as a bool and not a method

 you did
 
 bool KillPlayer()
 
 needs to be
  
 public void KillPlayer()
Comment
Add comment · Show 5 · 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 atr0phy · Nov 18, 2014 at 10:56 PM 0
Share

Uh...huh? That method declaration is perfectly valid. bool is the return type for his method, while omitting public/private/protected simply defaults to private. (The problem is that he is using yield)

avatar image Wizardman290 · Nov 18, 2014 at 11:32 PM 0
Share

Angry, I've tried this, in fact, that's what I've started with.

avatar image atr0phy · Nov 18, 2014 at 11:34 PM 0
Share

@Wizardman290 I posted a proper answer for ya, but it's awaiting mod approval. =/ $$anonymous$$inda aggravating that you have to jump thru hoops to get and give any help around here.

avatar image AlwaysSunny · Nov 18, 2014 at 11:57 PM 0
Share

@b1nary_atr0phy - $$anonymous$$eep being helpful and this too shall pass! :)

avatar image Wizardman290 · Nov 19, 2014 at 12:11 AM 0
Share

@b1nary_atr0phy - I know right? Whenever I ask one at night, it is declined overnight. Hey, I posted a comment on your question, any chance you could look at it? The error is gone, but nothing in that block works.

avatar image
0

Answer by koray1396 · Nov 18, 2014 at 11:04 PM

first, your bool method does not return any value as true / false. but the error showing is bool is not an iterator type and you can not use yield return new wait... blocks. check out IEnumerator and coroutines.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Death and Respawn?? 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceError, Tilemap Array with Transforms and Raycast (C# with Demo) 0 Answers

Android player crashes on startup 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