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
0
Question by G-Jewel · May 25, 2014 at 03:27 PM · c#noobscores

Implementing Scoring System

Hello there! I am pretty new to Unity and have been learning and using different tutorials on the web and youtube and reading a lot of advice here too. I wish to implement a scoring system. I know how to add a GUI Text but had no luck using different scripts to increase it. As of this moment the Player who touches any enemies on the screen are destroyed on collision. So when enemies are destroyed would like a score.

Here are the scripts. Playerscripts controls movement of player and collisions. Enemy scripts controls the weapons and lifetime of enemy on screen. Healthscript is self explanatory. Hope some one can help! Thanks

 using UnityEngine;
 
 /// <summary>
 /// Player controller and behavior
 /// </summary>
 public class PlayerScript : MonoBehaviour
 {
     void OnDestroy()
     {
         // Game Over.
         // Add the script to the parent because the current game
         // object is likely going to be destroyed immediately.
         transform.parent.gameObject.AddComponent<GameOverScript>();
     }
 
 
     /// <summary>
     /// 1 - The speed of the ship
     /// </summary>
     public Vector2 speed = new Vector2(50, 50);
     
     // 2 - Store the movement
     private Vector2 movement;
     
     void Update()
     {
         // ...
         
         // 6 - Make sure we are not outside the camera bounds
         var dist = (transform.position - Camera.main.transform.position).z;
         
         var leftBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(0, 0, dist)
             ).x;
         
         var rightBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(1, 0, dist)
             ).x;
         
         var topBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(0, 0, dist)
             ).y;
         
         var bottomBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(0, 2, dist)
             ).y;
         
         transform.position = new Vector3(
             Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
             Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
             transform.position.z
             );
         
         // End of the update method
 
         // 3 - Retrieve axis information
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
         
         // 4 - Movement per direction
         movement = new Vector2(
             speed.x * inputX,
             speed.y * inputY);
         
     }
     
     void FixedUpdate()
     {
                 // 5 - Move the game object
                 rigidbody2D.velocity = movement;
     }
 
     void OnCollisionEnter2D(Collision2D collision)
         {
             bool damagePlayer = false;
             
             // Collision with enemy
             EnemyScript enemy = collision.gameObject.GetComponent<EnemyScript>();
             if (enemy != null)
             {
                 // Kill the enemy
                 HealthScript enemyHealth = enemy.GetComponent<HealthScript>();
                 if (enemyHealth != null) enemyHealth.Damage(enemyHealth.hp);
 
         
                 damagePlayer = true;
             }
 
 
             // Damage the player
             if (damagePlayer)
             {
                 HealthScript playerHealth = this.GetComponent<HealthScript>();
                 if (playerHealth != null) playerHealth.Damage(0);
 
 
         }
 
 
      }
 
 }



 using UnityEngine;
 /// <summary>
 /// Handle hitpoints and damages
 /// </summary>
 public class HealthScript : MonoBehaviour
 {
 
     /// <summary>
     /// Total hitpoints
     /// </summary>
     public int hp = 1;
     
     /// <summary>
     /// Enemy or player?
     /// </summary>
     public bool isEnemy = true;
     
     /// <summary>
     /// Inflicts damage and check if the object should be destroyed
     /// </summary>
     /// <param name="damageCount"></param>
     public void Damage(int damageCount)
 
     {
         hp -= damageCount;
         
         if (hp <= 0)
 
 
         {
             // 'Splosion!
             SpecialEffectsHelper.Instance.Explosion(transform.position);
             // Dead!
             SoundEffectsHelper.Instance.MakeExplosionSound();
             Destroy(gameObject);
 
         
         }
     }
     void OnTriggerEnter2D(Collider2D otherCollider)
     {
         // Is this a shot?
         ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript>();
         if (shot != null)
         {
             // Avoid friendly fire
             if (shot.isEnemyShot != isEnemy)
             {
                 Damage(shot.damage);
                 
                 // Destroy the shot
                 Destroy(shot.gameObject);
 
                 // Remember to always target the game object, otherwise you will just remove the script
             }
         }
     }
 }



using UnityEngine;

 /// <summary>
 /// Enemy generic behavior
 /// </summary>
 public class EnemyScript : MonoBehaviour
 {
     private bool hasSpawn;
     private MoveScript moveScript;
     private WeaponScript[] weapons;
     
     void Awake()
     {
         // Retrieve the weapon only once
         weapons = GetComponentsInChildren<WeaponScript>();
         
         // Retrieve scripts to disable when not spawn
         moveScript = GetComponent<MoveScript>();
     }
     
     // 1 - Disable everything
     void Start()
     {
         hasSpawn = false;
         
         // Disable everything
         // -- collider
         collider2D.enabled = false;
         // -- Moving
         moveScript.enabled = false;
         // -- Shooting
         foreach (WeaponScript weapon in weapons)
         {
             weapon.enabled = false;
         }
     }
     
     void Update()
     {
         // 2 - Check if the enemy has spawned.
         if (hasSpawn == false)
         {
             if (renderer.IsVisibleFrom(Camera.main))
             {
                 Spawn();
             }
         }
         else
         {
             // Auto-fire
             foreach (WeaponScript weapon in weapons)
             {
                 if (weapon != null && weapon.enabled && weapon.CanAttack)
                 {
                     weapon.Attack(true);
                     SoundEffectsHelper.Instance.MakeEnemyShotSound(); 
                 }
             }
             
             // 4 - Out of the camera ? Destroy the game object.
             if (renderer.IsVisibleFrom(Camera.main) == false)
             {
                 Destroy(gameObject);
             }
         }
     }
     
     // 3 - Activate itself.
     private void Spawn()
     {
         hasSpawn = true;
         
         // Enable everything
         // -- Collider
         collider2D.enabled = true;
         // -- Moving
         moveScript.enabled = true;
         // -- Shooting
         foreach (WeaponScript weapon in weapons)
         {
             weapon.enabled = true;
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by Sprawl · May 25, 2014 at 03:44 PM

First, Please don't put all your script like that when asking a question. Just post what is necessary.

To make a score, create a public score variable somewhere in your game. You could put it in your character controls if you never delete it. Then, to add score when an ennemy die, you simply add score when you delete the gameobject.

Here's an example if you decide to add it in your controls :

In your controls scripts :

  public int score = 0;

Then in your ennemy script :

 OnDestroy() {
    myCharacterScript.score += 10;
 }
Comment
Add comment · Show 2 · 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 G-Jewel · May 25, 2014 at 08:11 PM 0
Share

It is possible you can modify the script so I can see which changes needed to be made in the correct places? Thanks

avatar image Sprawl · May 26, 2014 at 02:22 PM 0
Share

Sorry I didnt answer faster. I didnt read your code, there's a lot of stuff and I'd rather tell you how to do it than do it for you. Glad you found a solution !

avatar image
0

Answer by G-Jewel · May 26, 2014 at 02:20 PM

Never mind I have successfully implemented a bare bones scoring system which is not pretty as it uses the Unity GUI box. But hey does thejob!

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

New to Unity, Need Help. 1 Answer

How To Add PlayerPrefs Scores? 1 Answer

resetting object on collision 1 Answer

Parent Object Via Code C# 3 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