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 /
avatar image
0
Question by cgraf1 · May 10, 2016 at 06:01 AM · scripting problemtextinteger

Score with Text going up by random increments

Hello all,

I am currently trying to update my SCHMUP so that the player's score goes up by 100 when an enemy is destroyed. I have a Text in the Canvas which has a script managing the score that looks like this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreScript : MonoBehaviour {
 
     Text scoreWords;
 
     int playerScore;
 
     public int PlayerScore
     {
         get
         {
             return this.playerScore;
         }
         set
         {
             this.playerScore = value;
             UpdateScore();
         }
     }
 
 
     // Use this for initialization
     void Start () {
 
         scoreWords = GetComponent<Text>();
     
     }
     
     // Update is called once per frame
     void UpdateScore () {
 
         string scoreString = string.Format("{0:00000000}", playerScore);
         scoreWords.text = scoreString;
     
     }
 

I also have a script attached to each enemy that controls when they are destroyed that looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class Damage : MonoBehaviour {
 
     GameObject ScoreText;
 
     public int health = 1;
 
 
 
     bool isColliding;
 
 
 
     void Start(){
 
         ScoreText = GameObject.FindGameObjectWithTag("Score");
 
     }
 
     void OnTriggerEnter2D(){
 
         if(isColliding) return;
 
         isColliding = true;
         
         health--;
         
     }
 
     void Update(){
 
         isColliding = false;
 
 
         if(health <= 0){
             
             Die();
         }
     }
 
     void Die(){
 
         ScoreText.GetComponent<ScoreScript>().PlayerScore += 100;
         Destroy(gameObject);
         
     }
 
     
 }

The problem is, when an enemy is destroyed it randomly adds either 200, 300, or 400 points. Never 100. Also, enemies with health greater than 1 give points from all hits and not just from their destruction. If anyone could help me figure out why this is happening, I would be very grateful.

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

1 Reply

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

Answer by $$anonymous$$ · May 10, 2016 at 06:49 AM

Try to use the Update function as little as possible. All your actions are event driven so only activate them on that event. Check your health<=0 in the OnTriggerEnter2D function and call the Die function there.

Comment
Add comment · Show 3 · 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 cgraf1 · May 10, 2016 at 02:22 PM 0
Share

Thank you @$$anonymous$$. This helped for the inconsistency on the single hit players. One hit and it give 300 points every time. Why it is not 100, I don't know, but the number is arbitrary.. For enemies with more than one hit, however, they still give points on hits that aren't the final blow and those points are inconsistent, even though they use the same Damage script as the single hit enemies.

avatar image cgraf1 cgraf1 · May 10, 2016 at 08:03 PM 0
Share

I have solved this issue now. In case anyone was wondering, the reason for the random increments and the fact that points went up without destroying an enemy, was because I used the same Damage script on each of the bulletPrefabs I was using to destroy them on impact. So each hit brought up the score and when it destroyed an enemy it brought it up even more.

avatar image $$anonymous$$ cgraf1 · May 11, 2016 at 07:21 AM 0
Share

Good to hear you solved it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers

How do I insert a variable into a URL? Please Help! 1 Answer

Display the same string in multiple text boxes 1 Answer

I have a problem with Unity UI Text 0 Answers

Help with GUIStyle showing up 0 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