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 screenrace · Oct 25, 2014 at 09:33 PM · c#boolfalsetruecs

C# bool setting itself to true

Im trying to add a bleedout effect to my game. I made a bool called "isBleeding" and in the awake void, I set isBleeding to false. But when I start the game, it sets itself to true and I cant set it to false. In my death void, I also set isBleeding to false, and its still set to true. Heres my code.

using UnityEngine; using System.Collections;

public class HealthSystem_Network : MonoBehaviour {

 public float curHealth;
 public float maxHealth;
 public AudioClip deathClip;
 public GameObject iName;
 public GameObject cams;
 public GameObject iBarHealth;
 public string myname;
 public float bleedChance;
 public bool isBleeding;
 public float bleedSpeed;

 public AudioSource HurtAudioSource;
 public AudioClip[] hurtSounds;

 [HideInInspector] public float lostHealth;
 [HideInInspector] public float minHealth;
 [HideInInspector] public bool recovery;
 [HideInInspector] public float recoveySpeed;
 [HideInInspector] public float counter;
 [HideInInspector] public float waitTime;
 [HideInInspector] public float timer;
 [HideInInspector] public float resetAfterDeathTime;
 [HideInInspector] public bool dead;
 [HideInInspector] public float timerfS;
 [HideInInspector] public float timerS;
 [HideInInspector] public bool fS;
 
 void Awake () {
     lostHealth = 0;
     minHealth = 1;
     recovery = false;
     isBleeding = false;
     recoveySpeed = 15;
     waitTime = 7.0f;
     counter = waitTime;
     resetAfterDeathTime = 5.0f;
     cams = GameObject.Find("Main Camera");
     timerfS = 0.25f;
     timerS = timerfS;
     
     if(curHealth < 1)
         curHealth = 100;
     if(maxHealth < 1)
         maxHealth = 100;
 }
 
 public void uName (string me){
     networkView.RPC("userName", RPCMode.AllBuffered, me);
 }
 
 [RPC]
 public void userName (string nameOfUser){
     myname = nameOfUser;
 }

 public void Update () {
     if(curHealth < maxHealth && curHealth >= minHealth && !recovery) counter -= Time.deltaTime;
     if(counter <= 0 && !dead)recovery = true;
     if(counter > 0) recovery = false;
     if(recovery) curHealth += recoveySpeed * Time.deltaTime;
     if(curHealth >= maxHealth){
         counter = waitTime;
         curHealth = maxHealth;
     }
     if(curHealth < minHealth) Die();
     if(dead)timer += Time.deltaTime;
     else timer = 0;
     if(timer >= resetAfterDeathTime) LevelReset();
     lostHealth = maxHealth - curHealth;
     if(timerfS > 0) fS = true;
     else fS = false;
     if(dead) timerfS -= Time.deltaTime;
     else timerfS = timerS;
     if(iName && iBarHealth && cams) Info_Enemy();
     if (isBleeding = true)
                     Bleed ();
 }
 
 public void Recived (int fireDamage){
     networkView.RPC("Damage", RPCMode.All, fireDamage);
     counter = waitTime;
 }
 

 [RPC]
 public void Damage(int damage){
     if(curHealth >= minHealth && !dead) curHealth -= damage;
     audio.clip = hurtSounds[Random.Range(0, hurtSounds.Length)];
     audio.Play();
     //bleedChance = Mathf.Clamp(bleedChance, 0, 10);
     //return (Random.Range(0, 10) < bleedChance);
 }
 
 void Die(){
     if(curHealth < minHealth && curHealth != -300){
         dead = true;
         curHealth = -300;
         GetComponent<GTA_Controller_Network>().enabled = false;
         GetComponent<ToRagdollTest>().SendMessage("Dead");
         if(deathClip) AudioSource.PlayClipAtPoint(deathClip, transform.position);
         this.GetComponent<CapsuleCollider>().enabled = false;
         this.GetComponent<Rigidbody>().isKinematic = true;
         isBleeding = false;
     }
 }
 
 void LevelReset (){
     dead = false;
     curHealth = maxHealth;
     GetComponent<ToRagdollTest>().SendMessage("Dead");
     GetComponent<GTA_Controller_Network>().enabled = true;
     this.GetComponent<CapsuleCollider>().enabled = true;
     this.GetComponent<Rigidbody>().isKinematic = false;
     GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
     Transform randomSpawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)].transform;
     transform.position = randomSpawnPoint.position;
     isBleeding = false;
     Debug.Log("Respawned a player.");
 }

 void Bleed() {
             if (isBleeding = false)
                     recoveySpeed = 15;

             if (lostHealth <= 80)
                     isBleeding = true;

             if (isBleeding = true)
                     curHealth -= bleedSpeed * Time.deltaTime;
                     recoveySpeed = 0;

     }
 void Info_Enemy(){
     int heightLettter = iName.GetComponent<TextMesh>().fontSize;
     float enemyHealth_var_x = (curHealth * (heightLettter/30))/150  + curHealth /150;
     float enemyHealth_var_y = (heightLettter/60) + 0.7f;
     iName.transform.rotation = Quaternion.Euler(0, cams.transform.rotation.eulerAngles.y, 0);
     iBarHealth.transform.rotation = Quaternion.Euler(0, cams.transform.rotation.eulerAngles.y, 0);
     if(!dead) iBarHealth.transform.localScale = new Vector3 (Mathf.Lerp(iBarHealth.transform.localScale.x, enemyHealth_var_x, Time.deltaTime * 5), Mathf.Lerp(iBarHealth.transform.localScale.y,enemyHealth_var_y/10, Time.deltaTime * 5), iBarHealth.transform.localScale.z);
     else iBarHealth.transform.localScale = new Vector3 (Mathf.Lerp(iBarHealth.transform.localScale.x, 0, Time.deltaTime * 5), iBarHealth.transform.localScale.y, iBarHealth.transform.localScale.z);
     
     if(this.gameObject.name == "Remote_Player"){
         Vector3 distance = this.transform.position - GameObject.Find("Local_Player").transform.position;
         float limitOfVision = distance.magnitude;
         iName.GetComponent<TextMesh>().text = this.myname;
         iBarHealth.GetComponent<MeshRenderer>().enabled = true;
         if(limitOfVision <= 60 && !dead){
             iName.GetComponent<MeshRenderer>().enabled = true;
             if(limitOfVision >= 5) iName.GetComponent<TextMesh>().fontSize = (int)limitOfVision * 5;
             else iName.GetComponent<TextMesh>().fontSize = 25;
             float LimitVision = (limitOfVision * 0.01f) + 0.2f;

             switch (iName.GetComponent<OrientationName>().Sequence.currentSlot){
             case Setup.XYZ_Sequence.Width_Height_Depth_OR_Depth_Height_Width:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      Mathf.Lerp(iName.transform.localPosition.y, LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.z);
                 break;
             case Setup.XYZ_Sequence.Width_Depth_Height_OR_Depth_Width_Height:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      iName.transform.localPosition.y,
                      Mathf.Lerp(iName.transform.localPosition.z, LimitVision, Time.deltaTime * 5));
                 break;
             case Setup.XYZ_Sequence.Height_Width_Depth_OR_Height_Depth_Width:
                 iName.transform.localPosition = new Vector3 
                     (Mathf.Lerp(iName.transform.localPosition.x, LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.y, 
                      iName.transform.localPosition.z);
                 break;
             case Setup.XYZ_Sequence.Width_negativeHeight_Depth_OR_Depth_negativeHeight_Width:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      Mathf.Lerp(iName.transform.localPosition.y, -LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.z);
                 break;
             case Setup.XYZ_Sequence.Width_Depth_negativeHeight_OR_Depth_Width_negativeHeight:
                 iName.transform.localPosition = new Vector3 
                     (iName.transform.localPosition.x, 
                      iName.transform.localPosition.y,
                      Mathf.Lerp(iName.transform.localPosition.z, -LimitVision, Time.deltaTime * 5));
                 break;
             case Setup.XYZ_Sequence.negativeHeight_Width_Depth_OR_negativeHeight_Depth_Width:
                 iName.transform.localPosition = new Vector3 
                     (Mathf.Lerp(iName.transform.localPosition.x, -LimitVision, Time.deltaTime * 5), 
                      iName.transform.localPosition.y, 
                      iName.transform.localPosition.z);
                 break;
             }
         }
         else{
             iName.GetComponent<MeshRenderer>().enabled = false;
             iBarHealth.GetComponent<MeshRenderer>().enabled = false;
         }
     }
     else{
         iName.GetComponent<TextMesh>().text = null;
         iBarHealth.GetComponent<MeshRenderer>().enabled = false;
     }
 }

}

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

1 Reply

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

Answer by Unitraxx · Oct 25, 2014 at 09:35 PM

Classic error on line 75.

if (isBleeding = true) should be if (isBleeding == true)

Notice the = (assignment) versus == (equality operator)

Now we will dig somewhat deeper. You've made this same mistake at several places. (So be sure to check all of your code!) I also suggest to use if (isBleeding) and if(!isBleeding) instead of using equality operators.

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 screenrace · Oct 25, 2014 at 09:37 PM 0
Share

Ahhhh.... im so stupid. xD

avatar image Unitraxx · Oct 25, 2014 at 09:38 PM 0
Share

I made a small edit, you should actually not use equality operators at all for booleans. It's not wrong of course, but error prone and unnecessary.

avatar image Bunny83 · Oct 25, 2014 at 09:43 PM 0
Share

Yeah, classic ;)

@screenrace:
ps: If you want to debug a strange behaviour that is related to a certain variable, the first thing you should do is search for all uses of that variable and see if there's something wrong. In your case here there are 8 places where you use isBleeding.

All you if statements containing that variable are actually wrong. So you need to fix line 75, 122 and 128

avatar image screenrace · Oct 25, 2014 at 09:51 PM 0
Share

I fixed it now, but when the player dies from bleeding, he dies repeatedly?

avatar image Unitraxx · Oct 25, 2014 at 09:53 PM 0
Share

You can always add if(dead) return in the beginning of the Update method. You might also want to remove the instance of the dead player, it's up to you.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I make 2 objects that are close to each other being treated as being in the same position? 1 Answer

A bunch of errors that I can't fix 1 Answer

How do I switch a bool every two seconds? 3 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