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 Banksy · Mar 31, 2013 at 11:32 PM · updatescore

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); 
         }
     }

}

Comment
Add comment · Show 2
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 Banksy · Apr 01, 2013 at 07:33 PM 0
Share

I need to see your answer (not a comment) then I can click approve... :)

avatar image sdgd · Apr 01, 2013 at 07:41 PM 0
Share

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

Unity Answers Tutorial

1 Reply

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

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

Comment
Add comment · Show 14 · 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 Banksy · Apr 01, 2013 at 09:33 AM 1
Share

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..

avatar image Banksy · Apr 01, 2013 at 09:35 AM 1
Share

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.

avatar image sdgd · Apr 01, 2013 at 11:19 AM 0
Share

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

avatar image Banksy · Apr 01, 2013 at 06:23 PM 0
Share

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 ??

avatar image sdgd · Apr 01, 2013 at 06:41 PM 0
Share

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

Show more comments

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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