Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Add Points When Enemy Dies

Add Points When Enemy Dies

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Add Points When Enemy Dies on Tue Jul 21, 2015 8:10 pm

OneKnifeGames


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
So I have a script, and I want to add 1000 points when the Skeleton Hard.prefab gameobject is killed. I also want +1000 to be displayed for the character to see, any solutions?

Code:
using UnityEngine;
using System.Collections;


public class health : MonoBehaviour {
   //max health. you set this to what you want his health to be
   public int maxhealth=50;
   public int currenthealth;
   //cant die
   public bool invincible;
   public bool dead;
   //check if ou want to enable health regeneration
   public bool regenerate;
   //time between each generate
   public float regenerationtime=0.8f;
   //the timer
   private float regtimer;
   //amount of hp to regenerate per generate
   public int regenerationamount=2;
   private int healthsave;
   public bool givereward;
   private bool rewardgiven;
   public int xptogive=10;
   public int goldtogive=10;
   
   
   // Use this for initialization
   void Start () {
      givereward=true;
      
      AI ai=(AI)GetComponent("AI");
      if(ai) ai.health=maxhealth;
      
   currenthealth=maxhealth;
      healthsave=maxhealth;
   }
   
   // Update is called once per frame
   void Update () {
   
      
      
      //tell ai when health is changed
      if(healthsave>currenthealth|healthsave<currenthealth){
         AI ai=(AI)GetComponent("AI");
         if(ai){
         ai.health=currenthealth;
         healthsave=currenthealth;
         }
      }
      
      //death
      if(dead){
         AI ai=(AI)GetComponent("AI");
         //tell ai that he is dead
         if(ai)ai.dead=true;
      }
      else givereward=true;
      
      if(currenthealth>=maxhealth) currenthealth=maxhealth;
      
      //if health is less than zero
      if(currenthealth<=0){
         //check if invincible if not death
         if(invincible)dead=false;
         else dead=true;
         currenthealth=0;
      
      //regenerate
      if(regenerate){
         if(currenthealth<maxhealth&currenthealth>0){
      regtimer+=Time.deltaTime;
      if(regtimer>regenerationtime){
               currenthealth=currenthealth+regenerationamount;
               regtimer=0;
            }
      
      }
      }
   }
}
}

View user profile http://oneknifegames.weebly.com/

2 Re: Add Points When Enemy Dies on Mon Aug 31, 2015 10:44 am

aquilinajake


Member [Lv. 3]
Member [Lv. 3]
when the AI is dead talk to a GameManager Script or your score script, generally i do this by having a GameManager Empty Gameobject with a script on it called GameManager and have all the important game related code/variables here such as cash, score kills etc.



an easy way to do this would be adding the GameManager to your Ai so it has something to reference and a score variable on it like this



~Put this in your AI Code~

Code:


public GameObject = gm;


void Start()
{
gm = GameObject.Find("GameManager");
}


~Extend AI Code~

Code:

 if (dead)
        {
            AI ai = (AI)GetComponent("AI");
            //tell ai that he is dead
gm.GetComponent<GameManager>().points += 1000;
            if (ai) ai.dead = true;         

        }



~~create a canvas and add a Text Element to it (using unity 4.6+ or unity 5+)~~


~on a new Empty GameObject create a Script called GameManager~

Code:


//include the UI;
using UnityEngine.UI;



public int points;
public Text score;


void LateUpdate()
{
//this is how we will show the player their score.
score.text =  "Score: " + gm.Getcomponent<GameManager>().points;
}




add the new text UI element you created to the GameManager scripts, score in the inspector.

that is all.

View user profile http://jakeaquilina.weebly.com/

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum