Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 alexay007 · Apr 27, 2017 at 01:39 PM · unity 5scripting beginnervariablesenemies

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?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

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

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;
Comment
Add comment · Show 4 · 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 alexay007 · Apr 27, 2017 at 07:58 PM 0
Share

$$anonymous$$y code is in c#, it doesn't recognize "var"

avatar image Eno-Khaon alexay007 · Apr 27, 2017 at 08:15 PM 0
Share

Borrowing a line from your own script:

 var curSrc = src.Find (child.name);
avatar image alexay007 Eno-Khaon · Apr 27, 2017 at 08:32 PM 0
Share

It's true haha sorry, I'm new here. But anyway I can't make it work

Show more comments
avatar image
0

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.

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 alexay007 · Apr 27, 2017 at 08:00 PM 0
Share

I got this error: GameOver.cs(18,9): error CS0120: An object reference is required to access non-static member `GameOver.timer'

avatar image guido-paglie alexay007 · Apr 28, 2017 at 01:48 PM 0
Share

Ups! i am sorry i forgotted to tell to make timer static

private static float timer = 0.0f;

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate a GameObject slowly until reach some point 1 Answer

I can't give damage with these scripts 2 Answers

How to change UI image with a name to a different image with a name 2 Answers

How to change Texts in Unity 5 using UI Texts? 2 Answers

How to send simple values on local network? 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