- Home /
Timer that increases every time I kill an enemy
Well, I have a countdown script so that when it ends the scene changes:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameOver : MonoBehaviour
{
public string levelToLoad;
public float timer = 10f;
private Text timerSeconds;
void Start ()
{
timerSeconds = GetComponent<Text>();
}
void Update()
{
timer -= Time.deltaTime;
timerSeconds.text = timer.ToString("f2");
if (timer <= 0)
{
SceneManager.LoadScene(0);
}
}
}
I have another script for enemies dead:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DamageManager : MonoBehaviour
{
public GameObject[] deadbody;
public AudioClip[] hitsound;
public int hp = 100;
public int Score = 10;
private Vector3 velositydamage;
private float distancedamage;
void Start(){
}
void Update(){
if (hp <= 0) {
Dead (Random.Range (0, deadbody.Length));
}
}
public void ApplyDamage (int damage, Vector3 velosity, float distance)
{
if (hp <= 0) {
return;
}
distancedamage = distance;
hp -= damage;
velositydamage = velosity;
}
public void ApplyDamage (int damage, Vector3 velosity, float distance, int suffix)
{
if (hp <= 0) {
return;
}
distancedamage = distance;
hp -= damage;
velositydamage = velosity;
if (hp <= 0) {
Dead (suffix);
}
}
public void AfterDead (int suffix)
{
int scoreplus = Score;
if(suffix == 2){
scoreplus = Score * 5;
}
ScoreManager score = (ScoreManager)GameObject.FindObjectOfType (typeof(ScoreManager));
if(score){
score.AddScore (scoreplus, distancedamage);
}
}
// ** Important! for Ragdoll replacement
private AS_RagdollReplace ragdollReplace;
public void Dead (int suffix)
{
if (deadbody.Length > 0 && suffix >= 0 && suffix < deadbody.Length) {
// this Object has removed by Dead and replaced with Ragdoll. the ObjectLookAt will null and ActionCamera will stop following and looking.
// so we have to update ObjectLookAt to this Ragdoll replacement. then ActionCamera to continue fucusing on it.
GameObject deadReplace = (GameObject)Instantiate (deadbody [suffix], this.transform.position, this.transform.rotation);
ragdollReplace = deadReplace.GetComponent<AS_RagdollReplace> ();
// copy all of transforms to dead object replaced
CopyTransformsRecurse (this.transform, deadReplace);
// destroy dead object replaced after 5 sec
Destroy (deadReplace, 5);
// destry this game object.
Destroy (this.gameObject,1);
this.gameObject.SetActive(false);
}
AfterDead (suffix);
}
// Copy all transforms to Ragdoll object
public void CopyTransformsRecurse (Transform src, GameObject dst)
{
dst.transform.position = src.position;
dst.transform.rotation = src.rotation;
foreach (Transform child in dst.transform) {
var curSrc = src.Find (child.name);
if (curSrc) {
CopyTransformsRecurse (curSrc, child.gameObject);
}
}
}
}
How can I make that every time I kill an enemy, 10 seconds are added to the timer?
Answer by Nanousis · Apr 28, 2017 at 05:54 AM
You can make the timer a static float and reference it in your enemy death with this: Timer: Public static float time=10f; Death: Timer.time+=10;
Change the names on what suit you best and it will work perfectly
Answer by ShadyProductions · Apr 27, 2017 at 02:10 PM
You have to get the timerscript monobehaviour from the GameObject it is on to access the timer float variable from it.
you can make a reference to it like so: (object being the name of the gameobject the timerscript is attached to):
var timerScript = GameObject.FindWithTag("object").GetComponent<GameOver>();
and in your AfterDead method you can add:
timerScript.timer += 10f;
$$anonymous$$y code is in c#, it doesn't recognize "var"
Borrowing a line from your own script:
var curSrc = src.Find (child.name);
It's true haha sorry, I'm new here. But anyway I can't make it work
Answer by guido-paglie · Apr 27, 2017 at 02:32 PM
You can create a static function on GameOver called "OnEnemyKilled" and call it when an enemy is killed
public static void OnEnemyKilled()
{
timer += ADD_TIME_ENEMY_KILLED;
}
And then you can just call it GameOver.OnEnemyKilled
Also you can pass a paremeter and set the time you want to add.
I got this error: GameOver.cs(18,9): error CS0120: An object reference is required to access non-static member `GameOver.timer'
Ups! i am sorry i forgotted to tell to make timer static
private static float timer = 0.0f;