- Home /
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;
}
}
}
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.
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.
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
I fixed it now, but when the player dies from bleeding, he dies repeatedly?
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.