- Home /
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;
}
}
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);
}
}
}
}
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*
}
}
You're welcome. Please mark the answer to accepted that solved your question.