- Home /
C# score not updating
I am learning C#...
I have 2 scripts. One is on a pickup item ( a Heart object that adds points on pick up ) & an inventory script is attached to my main character .. each object has a collider.
For some reason my score is not updating when I pick up a heart..
I believe " c.gameObject.SendMessage("PickUpHealth", c); " in the Health.cs script is not calling function PickUpHealth..
So my question is why ??
here are both scripts
public class Health : MonoBehaviour {
public float rotationSpeed = 100.0f;
public AudioClip powerSound;
public UILabel guiLevel;
public float itemValue;
void OnTriggerEnter (Collider c) {
if (c.gameObject.tag == "Player") {
Debug.Log("HELLO");
c.gameObject.SendMessage("PickUpHealth", c);
AudioSource.PlayClipAtPoint (powerSound, transform.position);
Destroy(gameObject);
}
}
}
// My Character Inventory SCRIPT
public class Inventory : MonoBehaviour {
public static int powerup = 0;
public AudioClip powerSound;
private GameObject itemPowerup;
private float score;
public UILabel guiLevel;
void Start () {
score = 1.0f;
}
void PickUpHealth(Collider c){
if(c.CompareTag("heart")) {
Debug.Log (" Eddie collided with a heart ");
score += c.gameObject.GetComponent ().itemValue;
guiLevel.text = "Score: " + score;
// AudioSource.PlayClipAtPoint (powerSound, transform.position);
//Destroy(gameObject);
}
}
}
I need to see your answer (not a comment) then I can click approve... :)
my answer is below this comment ;)
we are commenting my answer you can't approve my comments but only answers
if you aren't sure how to
Answer by sdgd · Apr 01, 2013 at 08:21 AM
I don't understand how you want to do this but I'd do it this way:
public class Health : MonoBehaviour {
public float rotationSpeed = 100.0f;
public AudioClip powerSound;
public UILabel guiLevel;
public float itemValue;
void OnTriggerEnter (Collider c) {
if (c.gameObject.tag == "Player") {
Debug.Log("HELLO");
Inventory InvScript = c.gameObject.GetComponent("Inventory") as Inventory;
InvScript.score ++;
AudioSource.PlayClipAtPoint (powerSound, transform.position);
Destroy(gameObject);
}
}
}
OFC this inventory script HAS to be on a gameObject with tag Player
public class Inventory : MonoBehaviour {
public static int powerup = 0;
public AudioClip powerSound;
private GameObject itemPowerup;
public float score; // HAS TO BE PUBLIC
public UILabel guiLevel;
void Start () {
score = 1.0f;
}
}
OFC don't forget to set score to public otherwise you can't access it.
I'm sorry if I couldn't answer your Q for the .SendMessage but I've never used that and don't know what it does
but this way should work let me know if you run in to any errors
Edited the A
Hello sdgd,
Thanks for helping out...
I have updated the HEALTH with your changes.
The script runs well but the PickUpHealth function within the Inventory script is not working ?
When I say not working.. I'm receiving no errors which is good but.... for example I created a debug.Log "Eddie Collided.. " to test the script but it does not display in the console.. ins$$anonymous$$d the Health script plays a sound and destroys the game object... So I'm not sure if the health script is actually doing anything at all... ? The score is not updating..
I have stopped using Send$$anonymous$$essage as I read somewhere that it is very in-proficient. You are right GetComponent is a much better option.
you mean inventory script isn't doing anything?
void PickUpHealth(Collider c){
if(c.CompareTag("heart")) {
Debug.Log (" Eddie collided with a heart ");
score += c.gameObject.GetComponent ().itemValue;
guiLevel.text = "Score: " + score;
// AudioSource.PlayClipAtPoint (powerSound, transform.position);
//Destroy(gameObject);
}
actually yes this isn't doing anything this is a function and this is not on trigger enter at all you call it
I would delete this if I were you
as you've got everything in Health script
now that's strange why score is not updating
did you make it public?
and does GameObject with tag player have the inventory script on it?
BUT if you do want to access the function PickUpHealth
you need to make it public
public void PickUpHealth(Collider c){
when you call it from some else script you call it
Inventory InvScript = c.gameObject.GetComponent("Inventory") as Inventory;
InvScript.PickUpHealth(/*Collider*/);
but notice that c.gameObject HAS to be Object with the script attached on
I'll try and clean it up now... Basically the Inventory script attached to the character should get the " itemValue " that's attached to each Heart.
Its similar to the game PAC$$anonymous$$AN Imagine PAC$$anonymous$$AN(my character) picking up power pellets (Hearts) ... each Heart has a different score value..an " itemValue" that has been assigned to each heart object.. So there's a Health script on each Heart object. The inventory script is applied to the Character.. as he collides with a heart .... the inventory script should call the itemVAlue on that particular Heart & update the score accordingly... Now I think maybe the function on the Health script should be in fact on the Character's inventory script... yes ??
it has to be on the GameObject that's tagged for player in your case
otherwise I don't understand why didn't you get error as it couldn't find inventory
if my method did work please accept the answer as correct
Your answer
Follow this Question
Related Questions
Problem at add Score+1 When Pass rocks 0 Answers
iOS Lag From Updating Score 0 Answers
How to call a function only once in Update 1 Answer
Problem: Adding and Subtracting score 1 Answer
Score not updating... 1 Answer