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 BilliamJJohnson · Jul 29, 2014 at 10:09 PM · 2d-platformer

Adding Points When Enemy is Destroyed

**I need to update the Score when an Enemy is destroyed by my character. I'm new to C# but I do have some experience in programming. Here are my two scripts that I am working with... I would like to implement the "addPoints" function of ScoreScript into the part of HealthScript that destroys the enemy when the hp is 0.

Health for enemy**

 using UnityEngine;
 using System.Collections;
 
 public class HealthScript : MonoBehaviour {
     public int hp = 1;
     public bool isEnemy = true;
     
 
 
     public void Damage(int damageCount)
     {
         hp -= damageCount;
         
         if (hp <= 0) 
         {
             Destroy (gameObject);
 

 
         }
     }
     
     void OnTriggerEnter2D(Collider2D otherCollider)
     {
         ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript> ();
         if(shot != null)
         {
             if(shot.isEnemyShot != isEnemy)
             {
                 Damage(shot.damage);
                 Destroy(shot.gameObject);
             }
         }
     }
     }








Score Script

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour
 {
     public int score = 0;                    // The player's score.
 
 
     private PlatformerCharacter2D playerControl;    // Reference to the player control script.
     private int previousScore = 0;            // The score in the previous frame.
 
 
     void Awake ()
     {
         // Setting up the reference.
         playerControl = GameObject.FindGameObjectWithTag("Player").GetComponent<PlatformerCharacter2D>();
     }
 
     public void addPoints() {
                 score = score + 88;
         }
 
 
     void Update ()
     {
         // Set the score text.
         guiText.text = "Score: " + score;
 
         // If the score has changed...
         if(previousScore != score)
             // ... play a taunt.
 
 
         // Set the previous score to this frame's score.
         previousScore = score;
     }
 
 }
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
0

Answer by keburanuil · Jul 29, 2014 at 11:25 PM

Just get&store the score script into the enemy script. Now you can use all score script public functions and variables from enemy script.

 using UnityEngine;
 using System.Collections;
 
 public class HealthScript : MonoBehaviour {
     public int hp = 1;
     public bool isEnemy = true;
 
     private Score scoreScript;
 
     void Awake()
     {
         GameObject scoreObject = GameObject.Find("Your ScoreScript's GameObject name");
         scoreScript = scoreObject.GetComponent<Score>();
     }
 
     
     public void Damage(int damageCount)
     {
         hp -= damageCount;
         
         if (hp <= 0) 
         {
             scoreScript.addPoints();
 
             Destroy (gameObject);
             
         }
     }
     
     void OnTriggerEnter2D(Collider2D otherCollider)
     {
         ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript> ();
         if(shot != null)
         {
             if(shot.isEnemyShot != isEnemy)
             {
                 Damage(shot.damage);
                 Destroy(shot.gameObject);
             }
         }
     }
 }

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 BilliamJJohnson · Jul 30, 2014 at 04:48 PM 0
Share

Okay, so I added in the new lines that would allow me to use ScoreScript in HealthScript but I am still unable to call the function that I need that would make the Score work. Could it have something to do with the function "addPoints"?

 using UnityEngine;
 using System.Collections;
 
 public class HealthScript : $$anonymous$$onoBehaviour {
     public int hp = 1;
     public bool isEnemy = true;
     
 
 
     private ScoreScript scoreScript;
 
     void Awake()
     {
                 GameObject scoreObject = GameObject.Find ("Score");
                 scoreScript = scoreObject.GetComponent<ScoreScript> ();
         }
 
     public void Damage(int damageCount)
     {
         hp -= damageCount;
         
         if (hp <= 0) 
         {
             Destroy (gameObject);
             addPoints;    *<------This is what I want to call but it is still not working*
 
         }
     }


avatar image BilliamJJohnson · Jul 30, 2014 at 04:52 PM 0
Share

It worked. Thank you so much.

avatar image keburanuil · Jul 30, 2014 at 08:21 PM 0
Share

You're welcome. Please mark the answer to accepted that solved your question.

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

Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers

Draw modes in 2D 0 Answers

Problem with Jump Action C# 1 Answer

Missing Reference Exception: Box destoyed but crashes because still trying access it 1 Answer

How to make a 2D spritesheet 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