• 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
0
Question by KatieK · Apr 08, 2013 at 06:46 PM · scorescorescollect

score not updating

The basic concept is to collect objects, each time it collects a coin it increases the score by 5 and each time it collects the star it increases the score by 10.

I also have a collision script:

 function OnTriggerEnter(other:Collider)
 {
     SendMessageUpwards("addScore");
     Destroy(...);
 }

I have a control Script:

 public var score = 0;
 
          
    function collect()
     {
 .....

     }
     
 function addScore()
 {
     if (tag == "coin") {
        score = score + 5;
      }
     
    else if (tag == "star") {
     score = score + 10;
     }
     updateScore();    
 }
 
 function updateScore()
 {
     .....
 }

The score does not update when collected?

Comment
Add comment · Show 3
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 AlucardJay · Apr 08, 2013 at 06:48 PM 0
Share

Step 1 : read this page : http://answers.unity3d.com/page/newuser.html

Duplicate Question : http://answers.unity3d.com/questions/434175/how-to-access-a-function-from-another-script.html

Please Delete this Question.

Please be patient if your question/reply doesn't show straight away.

As a new user, your posts and questions are held in a moderator que until it is approved and then it is displayed. When your karma rises, you'll be able to post questions, comments and answers without waiting for someone to approve it.

http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image KatieK · Apr 08, 2013 at 06:51 PM 0
Share

sorry the question is asking something else now as the first one was changed slighty and that was about accessing the funciton but this is about the score

avatar image AlucardJay · Apr 08, 2013 at 06:57 PM 0
Share

Ok, I shall give the benefit of the doubt.

Your problem probably lies here :

 if (tag == "coin")

do you mean

 if ( gameObject.tag == "coin" )

Also do some debugging. Are you sure it isn't the same problem? Is the function actually being called?

 function addScore()
 {
     Debug.Log( gameObject.name + " just had addScore called" );
     if ( gameObject.tag == "coin" ) {
        // ....

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AlucardJay · Apr 08, 2013 at 07:12 PM

Hang on, I need to understand your setup.

So collisionDetector.js is on the spawned object (coin or star)

the coin/star is a child of the object with the spawnControl.js

So SendMessageUpwards is working.

Now .... how is the spawnControl supposed to know what child the message came from?

 if (coin) // you say works, yes but not for the reasons you think

this means if ( coin : GameObject is not equal to null ) , that's why it functions, but it is wrong.

Look at the parameters that can be used in the SendMessageUpwards command :

 SendMessageUpwards (methodName : String, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver)

So, you can send a value when calling a function . Try :

 function OnTriggerEnter(other:Collider)
 {
     Debug.Log( gameobject.tag + " is sending a message to the spawnScript" );
     SendMessageUpwards("addScore", gameObject.tag);
     Destroy(gameObject);
 }

then :

 function addScore( theTag : String )
 {
     Debug.Log( "A message was just received by " + theTag );
     if (theTag == "coin") {
         score += 5;
         updateScore();
     }
 
     else if (theTag == "star") {
         score += 10;
         updateScore();
     }    
 }

Do you understand what is happening here?

This is using SendMessage to send a value to the called function. The function gets that value as a String, and then checks what that value is.

Comment
Add comment · Show 4 · 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 AlucardJay · Apr 08, 2013 at 07:51 PM 0
Share

Ok so this is where using Debug really shines. I have updated my answer, but here are the changes using Debug.Log :

 function OnTriggerEnter(other:Collider)
 {
     Debug.Log( gameobject.tag + " is sending a message to the spawnScript" );
     Send$$anonymous$$essageUpwards("addScore", gameObject.tag);
     Destroy(gameObject);
 }

then :

 function addScore( theTag : String )
 {
     Debug.Log( "A message was just received by " + theTag );
     if (theTag == "coin") {
         score += 5;
     }
  
     else if (theTag == "star") {
         score += 10;
     }

     updateScore();
 }

Check your console when you collect an item. What is the gameObjects tag that is sending the message, and what is the spawnScript saying it is hearing?

avatar image AlucardJay · Apr 08, 2013 at 07:54 PM 0
Share

I think you have your tags not set up, and because you are only checking for a tag, and even if the tag is not coin or star, you are still calling updateScore();

try :

 function addScore( theTag : String )
 {
     Debug.Log( "A message was just received by " + theTag );
     if (theTag == "coin") {
         score += 5;
         updateScore();
     }
 
     else if (theTag == "star") {
         score += 10;
         updateScore();
     }    
 }

now updateScore(); shall only be called when the tag matches on of the conditional checks. Definitely start using Debug, it shows exactly where you are in the script, and what is happening.

avatar image AlucardJay · Apr 08, 2013 at 07:57 PM 0
Share

Actually, that doesn't matter I just realized, updateScore(); is only modifying the GUI text. sorry (it is very late for me). But still, debug and see what is happening.

avatar image Unitraxx · Apr 08, 2013 at 08:26 PM 0
Share

Your spawnControl script, to what object is it attached?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

I need help making a score system for a First Person Controller 1 Answer

How to check if variable is equal to range of integers 2 Answers

Need Help with Dual Scoring System C# (score over time and kill score) 1 Answer

How to set my score to 0 when hit restart ; 1 Answer

creating score point for 3 objects 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