- Home /
got a kind of health(battery) pickup, that doesnt work
so i got 2 scripts one for on my gamemanger (empty object) and the other for the pick up, when i pick it up it should send a message that my curBattery = 100, but it doesnt do anything? could anybody help me.
pickup using UnityEngine; using System.Collections;
public class Pick: MonoBehaviour {
public BatteryLife batteryLife;
//public float maxBattery = 100.0f;
//public float curBattery = 100.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
float curBattery = 100 ;
//float maxBattery = 0;
void OnTriggerEnter(Collider coll)
{
if(coll.gameObject.tag == "pickUp")
{
coll.gameObject.SetActive (false);
//curBattery=maxBattery;
batteryLife.SendMessage("Drain",curBattery,SendMessageOptions.DontRequireReceiver);
}
}
}
gamemanager
using UnityEngine; using System.Collections;
public class BatteryLife : MonoBehaviour { public float maxBattery = 100.0f; public float curBattery = 100.0f; public float batteryDrainSpeed = 500.0f; public string battery = "Battery: ";
void Start () {
}
void Update () {
//curBattery = Mathf.Clamp(curBattery - (Time.deltaTime * (1000 / batteryDrainSpeed)),0.0f,maxBattery);
Drain ();
if (curBattery <= 0)
{
Death ();
}
}
void OnGUI() {
GUI.Box(new Rect(140, 175, Screen.width / 10 /(maxBattery / curBattery), 25), battery + curBattery + "/" + maxBattery);
}
void Death(){
if (curBattery <= 0)
{
Application.LoadLevel("Death");
}
void Drain(){
curBattery = Mathf.Clamp(curBattery - (Time.deltaTime * (1000 / batteryDrainSpeed)),0.0f,maxBattery);
}
}
}
Answer by korbul · Jun 11, 2014 at 01:11 PM
Make sure that your entities respect this table http://answers.unity3d.com/storage/attachments/2050-interactionmatrix.png
You can cross rows with columns in that table, if there is a Y in the cell then you will have OnTriggerEnter callback for those two objects.
secondly, make sure the other collider has a "pickUp" tag set on it.
if(coll.gameObject.tag == "pickUp")
also Death() function does not have its brackets in order.
void Death(){
if (curBattery <= 0)
{
Application.LoadLevel("Death");
}
void Drain(){
curBattery = Mathf.Clamp(curBattery - (Time.deltaTime * (1000 / batteryDrainSpeed)),0.0f,maxBattery);
}
}
it looks like Drain() is inside Death()
Hope these suggestions help
no it still doesnt seem to be working andi solved the death thing, accidentily deleted it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Very Simple Pickup Issue 0 Answers
pick up item collision 2 Answers
Picking Up AND Hold Onto Items using Raycasting (Bug) 2 Answers
Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 Answers
Trigger event from ground and activate the Object-child in the Camera 1 Answer