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 pedromrnosorio97 · Jul 12, 2019 at 12:16 PM · not workingscore systemwave

Basic scoring not working

hi everyone, im trying to do a wave system with a score value, enemy dies increment 1 to the wavecounter and when the wave counter is equal to the wavelimit start next wave(which i havent created but thats another topic). Basically saying when the wave limit is reached start next wave.

Here´s the enemy code(the place where im trying to implement) :

public class Enemy : MonoBehaviour {

 public float EnemySpeed;

 public float EnemyStoppingDistance;

 public float EnemyReatreatDistance;

 private float TimeBetweenShots;

 public float StartTimeBetweenShots;

 public int Health;

 public GameObject Projectile;

 private Transform Target;

 public GameObject DeathEffect;

 private GameObject objSpawn;

 public int WaveLimit;

 public int WaveCounter;
   



 void Start()
 {
     Target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
     TimeBetweenShots = StartTimeBetweenShots;
     objSpawn = (GameObject)GameObject.FindWithTag("Spawner");
 }


 void Update()
 {
     if (Vector2.Distance(transform.position, Target.position) > EnemyStoppingDistance)
     {
         transform.position = Vector2.MoveTowards(transform.position, Target.position, EnemySpeed * Time.deltaTime);
     }
     else if (Vector2.Distance(transform.position, Target.position) < EnemyStoppingDistance && Vector2.Distance(transform.position, Target.position) > EnemyReatreatDistance)
     {
         transform.position = this.transform.position;
     }
     else if(Vector2.Distance(transform.position, Target.position) < EnemyReatreatDistance)
     {
         transform.position=Vector2.MoveTowards(transform.position, Target.position, -EnemySpeed * Time.deltaTime);
     }

     if (TimeBetweenShots <= 0)
     {
         Instantiate(Projectile, transform.position, Quaternion.identity);
         TimeBetweenShots = StartTimeBetweenShots;
     }
     else 
     {
         TimeBetweenShots -= Time.deltaTime;
     }

     if(Health<= 0)
     {
         
         Instantiate(DeathEffect, transform.position, Quaternion.identity);
         Destroy(gameObject);
         
        
     }


 }
   public void OnDestroy()
   {
     
     WaveCounter++;
     if (WaveCounter == WaveLimit)
     {
         Debug.Log("WaveLimit reached");
     }
   }

 public void TakeDamage(int Damage)
 {
     Health -= Damage;
 }

}

the exact place where the score system is is:

public void OnDestroy()

{

  WaveCounter++;
  if (WaveCounter == WaveLimit)
  {
      Debug.Log("WaveLimit reached");
  }

} This log i put isnt appearing thats why i detected a problem.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by I_Am_Err00r · Jul 12, 2019 at 02:57 PM

I don't see where you are calling OnDestroy, Destroy(gameObject) is a Unity function and will not call OnDestroy for you.

Try this:

 if(Health<= 0)
      {
          OnDestroy();
          Instantiate(DeathEffect, transform.position, Quaternion.identity);
          Destroy(gameObject);          
      }

I don't know if how the waves progress this is where you would want to call OnDestroy, but you need to call that function yourself.

Let me know if that is the problem.

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 pedromrnosorio97 · Jul 12, 2019 at 04:43 PM

the message is showing but only thing is that the counter number doesnt go up and if i try to set the limit to 2 or 3 it shows even if i didnt reach the limit, thanks for responding im very new at unity and programming so im very apreciative of that.

Comment
Add comment · Show 6 · 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 I_Am_Err00r · Jul 12, 2019 at 04:51 PM 0
Share

I'm confused by what you are saying here.

Give me step by step what it is doing, and then step by step what you need it to do.

avatar image pedromrnosorio97 · Jul 12, 2019 at 05:02 PM 0
Share

what is happening is that when i kill an enemy it shows the message. what i wanted to happen is the message to be shown when the wavecounter is equal to the wave limit in this case when i set the wavelimit to 3 i want to kill 3 enemies and the message would be showned.

avatar image I_Am_Err00r pedromrnosorio97 · Jul 12, 2019 at 05:11 PM 0
Share

Did you setup the waveLimit in the inspector? It's set to public, so you can either hardcode it in the script at start or something, or just put 3 in the spot in the inspector for waveLimit.

avatar image pedromrnosorio97 · Jul 12, 2019 at 06:17 PM 0
Share

yes i putted the value on the ispector thats the part that is bugging me

avatar image I_Am_Err00r pedromrnosorio97 · Jul 12, 2019 at 06:46 PM 0
Share

Debug.log is the next step here if you are certain that the wavelimit is set.

 void Start()
  {
      Target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
      TimeBetweenShots = StartTimeBetweenShots;
      objSpawn = (GameObject)GameObject.FindWithTag("Spawner");
      Debug.log(WaveLimit);
  }

and then again here to make sure they match:

 WaveCounter++;
 Debug.log(WaveLimit);
   if (WaveCounter == WaveLimit)
   {
       Debug.Log("WaveLimit reached");
   }

And just to be safe, let's add a log here too, and maybe move the OnDestroy() line towards the bottom of that:

 if(Health<= 0)
       {
           Debug.log("The health just reached zero, is this appearing more than once per enemy?");
           Instantiate(DeathEffect, transform.position, Quaternion.identity);
           Destroy(gameObject);
           OnDestroy();
       }

If everything is setup correctly, you should get a log at start clarifying with the WaveLimit is, if it says anything but what you put in the inspector or if it is correct at start and somehow changes when you kill an enemy, then you have another script somewhere that is accessing that script and changing the value;

avatar image pedromrnosorio97 I_Am_Err00r · Jul 15, 2019 at 10:31 AM 0
Share

sorry for the late response i was way for the weekend. I did what you said and what happened was this: -At start it shows the wave limit, 2 in this case -When i kill 1 enemy it shows the health has reached zero message -Then appears 2 more messages with 2, which i think is the wave limit -Then appears the message Wave limit reached also -Everytime an enemy spawns in it shows the message "2" whic again i think is the wavelimit

Perhaps the problem is the fact that i putted in my enemy script?

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

111 People are following this question.

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

Related Questions

score display not showing 1 Answer

Unity 2017.1.0f3 Failing To Start 0 Answers

Latest SDK not working 0 Answers

Issues with script in unity 5.4.5p5 and VS 2017 1 Answer

UI Text not changing color [C#] 2 Answers


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