Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 digs · May 07, 2014 at 11:21 PM · instantiate

Instantiated enemies not adding to kill count

Hello, I've seen a few posts about this, but nothing seems to be working

I have an "enemy" preFab and a "bullet" preFab, the bullets fire out when you click fire1, and the enemies die (Destroy) when the bullets collide with them. The enemies are being created with IEnumerator + StartCoroutine

I'm trying to retrieve when the enemy is destroyed - 6 enemies spawn at once

 void OnCollisionEnter (Collision other)
 {
     rigidbody.useGravity = true;
     rigidbody.AddForce(0,-100,0);
 
     if(other.gameObject.tag == "enemy")
     {
         newKill = true;
         Debug.Log("HIT");
         killCount = 1;
     }
 }

That returns 6 "HIT"'s when the 6 enemies die, however the bool "newKill" doesn't change from false to true in the inspector - I initialize it like so:

 public bool newKill = false;

which I think is the problem, I can't get this data to cross classes, which would allow me increment a killCounter in another class - "GameData"

 if(newKill)
 {
     killCounter += 1;
     newKill = false;
 }

I think the problem is because the objects are instantiated and destroy to quickly??

Any thoughts?

Comment
Add comment · Show 1
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 digs · May 07, 2014 at 11:53 PM 0
Share

I adjusted the size of the sphere collider surrounding my enemies, and only a few add to the kill count when they die - so I know data is being sent to my killCounter, but why not all of them?

here is the code i'm using to spawn the enemies

void Play() /**/ { StartCoroutine (SpawnWaves()); }

 IEnumerator SpawnWaves()
 {
     yield return new WaitForSeconds (wait);
     while(true)
     {
         for(int i = 0; i <= enemyCount; i++)
         {
             Vector3 spawnPosition = new Vector3 (Random.Range (48, 55), spawnValues.y, Random.Range (-spawnValues.z, spawnValues.z));
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate (enemy, spawnPosition, spawnRotation);
             //yield return new WaitForSeconds (delay * Time.time);
         }
         yield return new WaitForSeconds (waveDelay);
         wave++;
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xt-xylophone · May 07, 2014 at 11:54 PM

In your killCounter class just create a public function that can be called by your enemies in their OnCollisionEnter function.

Something like:

 public void incrementKillCounter() {
     killCounter += 1;
 }

And in your enemy class simply call that function. You will need to save an instance of GameData for your enemy to call it though or make everything static.

Comment
Add comment · Show 1 · 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 digs · May 08, 2014 at 06:29 AM 0
Share

Perfectly simple, Thank you!

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

21 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

Related Questions

Y value wont work? 1 Answer

Enemy Health Damage 2 Answers

Cloning Problem Script 0 Answers

Prefab connection problem 1 Answer

Instantiate prefabs working in editor but not in builds 0 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