Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by BBoffa · Jul 29, 2016 at 07:00 PM · c#3dgamepicture

Score won't work

alt text

I'm trying to make a game and my score won't work. The int is showing that the score is 3. But the text still says zero here's my code:

Score: using UnityEngine; using UnityEngine.UI; using System.Collections;

public class Score : MonoBehaviour { public int score; public Text scoreTxt;

 // Use this for initialization
 void Start () {
     score = 0;
 }
 
 // Update is called once per frame
 void Update () {
     scoreTxt.text = "Score: " + score;
 }

} Now CoinCollision: using UnityEngine; using System.Collections;

public class CoinCollision : Score {

 public void init()
 {
     scoreTxt.text = score.ToString ();
 }
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void OnTriggerEnter(Collider Col) {
     if (Col.tag == "Player") {
         score = score + 1;
         Debug.Log ("+1");

     }
 }

} Any ideas please let me know as soon as possible.

screenshot-17.png (107.9 kB)
Comment
Add comment · Show 11
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 Masterio · Jul 29, 2016 at 07:47 PM 0
Share

Class extending is useless here. You can make a score property static, but i not suggest it. Try to separate it. I wrote example:

Example:

 /// This class keeps a score and update a text.
 /// Attach it to the single game object.
 public class Score : $$anonymous$$onoBehaviour 
 { 
     public int score; 
     public Text scoreTxt;
 
     void Start () 
     {
         score = 0;
     }
 
     void Update () 
     {
         scoreTxt.text = "Score: " + score;
     }
 }
 
 /// This class is attached to the coin. It sends a score + 1 if player touch it.
 public class CoinCollision : $$anonymous$$onoBehaviour 
 {
     public Score scoreHandler;        // you need to put a score game object here (in inspector). You should make a game logic for this kind of fields (some singleton fe).
 
     void OnTriggerEnter(Collider col) 
     {
         if (col.tag == "Player") 
         {
             scoreHandler.score ++;
             
             Destroy(gameObject); // you should destroy it after touch.
         }
     }
 }





avatar image BBoffa Masterio · Jul 29, 2016 at 09:15 PM 0
Share

What would you put to be the gameObject from my screen? Or should I make one?

avatar image BBoffa Masterio · Aug 13, 2016 at 07:18 AM 0
Share

I need your help, $$anonymous$$asterio. How would I make a bullet shoot forward? Could you make a c# script and if it doesn't work I'll send you my project and you can see what is wrong and tell me or you can edit it and send it back to me. Please do this as soon as possible!

avatar image Masterio · Jul 30, 2016 at 08:06 AM 0
Share

Simply create a new GameObject: call it "Score" and attach the Score script to it. Next place the Score object in scoreHandler field in every CoinCollision object.

The Score game object must be the only one object on the scene with the Score component.

avatar image BBoffa Masterio · Jul 30, 2016 at 10:05 AM 0
Share

If the player collided with the coin how would the score go up? Is the Coin the score gameObject you are on about? I don't understand what you mean could you make a unity project and do the score thing and take a screenshot and send it to me please. Thank you.

avatar image Masterio · Jul 31, 2016 at 04:50 PM 0
Share

https://mega.nz/#!TshHXZpY!9BZmnCwGXNI8GVDIE2scjVx8tO9yT14tWYIDF7ZNX7g

thera are two ways with singleton or with handlers

avatar image BBoffa Masterio · Jul 31, 2016 at 08:37 PM 0
Share

It works but it still won't update I'll send it to you: https://www.dropbox.com/sh/gapkyisfidfyk8o/AAALfbEg9IiXqy6uZtiYwu3va?dl=0

avatar image Masterio · Aug 01, 2016 at 08:20 AM 0
Share

You missed: alt text

This is your reworked project lil bit. Added Game singleton, now you dont need to find Score handlers anymore.

https://mega.nz/#!3hoFWLgI!R9bJ2q4B4YXj$$anonymous$$r29ZVqSIcWN5Su4t7bPQWexfPL0WPA

advice.jpg (286.1 kB)
avatar image BBoffa Masterio · Aug 01, 2016 at 08:37 AM 0
Share

I cannot say how much you have helped me with this. Thank you very much! What type of games do you make and could test one sometime?

avatar image Masterio · Aug 01, 2016 at 09:05 AM 0
Share

I'am glad :) Press reward button if the answer helped you ;) I'm still looking for my direction, now i am works on 2D platformer game :)

avatar image BBoffa Masterio · Aug 01, 2016 at 09:17 AM 0
Share

I don't have any points also as you replied I can't you have to put an answer just like cdnDave did. If you are making any other games and you want someone to test it, let me know and I would sure enjoy testing your games.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cdnDave · Jul 29, 2016 at 07:40 PM

It appears that the issue here is that the Update function in the base class (Score) isn't getting called because it's hidden by the Update function in the derived class(Coin Collision). Since the Update function in the derived class is empty anyway you should just be able to remove it. Doing that will unhide the base class Update function and it will be called every frame as you expect. The takeaway from this is that when you define a function in a derived class that already exists in the base class you're basically replacing the base class function. It seems that you're also defining a Start function in both classes, I expect that the base class start function is also not getting called.

Edit: From the looks of it you only have one coin right now but if you add more coins you are going to need to move the score handling to it's own script, right now each coin basically has it's own score instead of there being a single score that each coin increments.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Why do my scene view and game view models look so different?,Why do my 3D Models look so distorted in game mode compared to scene mode? 0 Answers

C# - JS problem. 1 Answer

Explosion on contact 0 Answers

How do you rotate the player and keep them there? 1 Answer

Preventing A Teleporting GameObject From Passing Through Walls 2 Answers


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