Can't change int Value (C#)
This may be me being stupid, but I'm trying to make the flashlight battery increase to make a hundred (when you pick up a batter pack) but it stays on the battery it was already on
(flBattery is the battery count)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Battery : MonoBehaviour {
public float flBattery = 100;
public Text batteryTxt;
public Light flashlight;
public Image bHigh;
public Image bMed;
public Image bLow;
public Image bNone;
public float batteryDrainSpeed = 0.15f;
public GameObject batteryPickup;
public Text pickUpText;
public Light lightSrc;
private int flashInt;
// Use this for initialization
void Start () {
pickUpText.enabled = false;
StartCoroutine ("BatteryLoss");
bMed.GetComponent<Image> ().enabled = false;
bLow.GetComponent<Image> ().enabled = false;
bNone.GetComponent<Image> ().enabled = false;
}
IEnumerator BatteryLoss() {
while (flBattery > 0) {
yield return new WaitForSeconds (batteryDrainSpeed);
flBattery--;
batteryTxt.text = (flBattery + "%");
Debug.Log (flBattery);
}
}
void OnTriggerEnter (Collider other) {
flashlight.intensity = 3.0f;
flBattery = 100;
pickUpText.GetComponent<Text> ().enabled = true;
StartCoroutine (pickUpFunc());
}
IEnumerator pickUpFunc() {
yield return new WaitForSeconds (2);
GameObject.Destroy (batteryPickup);
pickUpText.GetComponent<Text> ().enabled = false;
}
}
Answer by aditya · Aug 12, 2016 at 01:12 PM
There could be many reasons for this ... like
One of your gameobjects, either your batter pack or the object that is trying to pick up this pack, should have rigidbody on it
the collider of the gameobject this script is attached on is not a trigger
script is on a wrong gameobject
if flBattery is a floating point number then where is the
f
like 100f or 100.0fPlus, if your OnTriggerEnter function is not in need of the collider info then why are you giving it as a parameter, you can remove it and save some computation
Answer by NerdClown · Aug 12, 2016 at 01:13 PM
This isn't just a matter of the script being a MonoBehaviour is it?
When you've got it on a GameObject in the scene and have typed in some different value in the inspector yourself (or have one from a previous version of the script), that value will overwrite the default value you set in the script.
Could be that, I remember it confused me a lot when I first started out with Unity.
Your answer
Follow this Question
Related Questions
I can't figure out why my score doesn't save/increase 0 Answers
Unity Setting Integer to 0 continuously? 0 Answers
Changing GUI Text to int and applying operands 1 Answer
Add a Max int value? 2 Answers