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 aaronmhargrove · May 10, 2016 at 06:01 AM · spawnspawningwhile-loopcondition

Spawn enemy while player is alive....

I am trying to spawn enemies while the player is still alive, however nothing is being spawned at all. Any Ideas why? I am new to programming so if it is a simple fix please forgive me :)

Here is my code

 public class CallSpawn : MonoBehaviour
 {
     public Transform astroid;
     public Vector3 position;
     public int count = 0;
 
     public void Spawn()
     {
         while (GameMaster.isDead == 0)
         {
             count = count + 1;
             if (0 == (count % 10))
             {
                 position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
                 Instantiate(astroid, position, Quaternion.identity);
             }
 
         }
     }
 }

Class being called

 using UnityEngine;
 using System.Collections;
 
 public class GameMaster : MonoBehaviour {
     public static int isDead = 0;
     public static void KillPlayer(Player player)
     {
         Destroy(player.gameObject);
         isDead = 1;
     }
     public static void KillEnemy(Enemy astroid)
     {
         Destroy(astroid.gameObject);
     }
     
 }


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
1
Best Answer

Answer by tpusch · May 10, 2016 at 04:27 PM

The biggest problem I see is that your spawn function is an infinite loop, after entering the while GameMaster.isDead can never change from 0. Also my guess is that you are trying to use your count as a delay timer between spawns, if that is the case you can put something like this in the CallSpawn update and never have to worry about manually calling spawn.

 private float spawnDelay = 10;
 private float timeUntilSpawn = 0;

 void Update(){
   if(GameMaster.isDead == 0){
     if(timeUntilSpawn <=0{
            position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
            Instantiate(astroid, position, Quaternion.identity);
            timeUntilSpawn = spawnDelay
     }else{
         timeUntilSpawn -= Time.deltaTime;
     }
   }
 }

One other note to help readability would be to change isDead from an int to a bool if you only have dead or alive.

 public static bool isDead = false;
 public static void KillPlayer(Player player){
     isDead = true;
 }

And then you don't have to worry about comparing to 1 or 0 in your checks with it, simply if(GameMaster.isDead) or if(!GameMaster.isDead)

Comment
Add comment · Show 5 · 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 aaronmhargrove · May 10, 2016 at 09:07 PM 0
Share

Okay thank you! this does work but the clones do not kill me like the parent does.... dimple fix, or do i need to scrap something and restart?

avatar image tpusch aaronmhargrove · May 10, 2016 at 09:12 PM 0
Share

Probably that astroid, asteroid?, should be a GameObject rather than a Transform. Which I missed earlier.

avatar image aaronmhargrove tpusch · May 10, 2016 at 09:54 PM 0
Share

Okay, I changed them to GameObjects but I have to change my Player kill code now too. I looked up the code (I was using distance formula) but the code I found on the wiki did not work. Sorry if this is a very simple problem. I'm new to program$$anonymous$$g and even newer to C# and Unity. Here is the code (code that is commented out is the old method I was using)

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class Player : $$anonymous$$onoBehaviour
 {
     public GameObject player;
     public GameObject astroid;
     public int fallBoundary = -10;
 
  
     void OnCollisionEnter2D(Collision2D coll)
     {
         if (coll.gameObject.tag == "astroid")
         {
             Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
         }
       
     }
 
 }
 
 //public class Player : $$anonymous$$onoBehaviour
 //{
 //    public Transform player;
 //    public Transform astroid;
 //    public int fallBoundary = -10;
 
 //    public void Update()
 //    {
 //        if ((player != null) && (astroid != null))
 //        {
 //            if (transform.position.y < fallBoundary)
 //            {
 //                Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
 //            }
 //            if ((player.position.x == astroid.position.x) && (2 > (astroid.position.y - player.position.y)))
 //            {
 //                Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
 //            }
 //            else if ((player.position.y == astroid.position.y) && ((2 > (astroid.position.x - player.position.x)) || (2 > (astroid.position.x - player.position.x))))
 //            {
 //                Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
 //            }
 //            else if (2 > ($$anonymous$$ath.Sqrt($$anonymous$$ath.Pow(player.position.x - astroid.position.x, 2) + $$anonymous$$ath.Pow(player.position.y - astroid.position.y, 2))))
 //            {
 //                Game$$anonymous$$aster.$$anonymous$$illPlayer(this);
 //            }
 //        }
 
 //    }
 
 //}
Show more comments
Show more comments
avatar image
1

Answer by JoshDangIt · May 10, 2016 at 04:55 PM

You'll want something more like this: (Untested)

 public class CallSpawn : MonoBehaviour
 {
     public GameObject astroid; //this should be a GameObject rather than a transform. 
     public float spawnRate = 10f; //enemy spawn rate in seconds
 
     private bool shouldSpawn = false;
     private bool nextSpawnTime;
     
     public void StartSpawning()
     {
         nextSpawnTime = Time.time + spawnRate;
         shouldSpawn = true;
     }
 
     void Update() //update is called once each frame
     {
         if(shouldSpawn == false) return; //do not run the below code if the enemies shouldnt spawn
 
         if(GameMaster.isDead == true) //replace GameMaster.isDead with a bool. It's much cleaner.
         {
             shouldSpawn = false;
         }
 
         if(Time.time > nextSpawnTime)
         {
             Vector3 position = new Vector3(Random.Range(-30.0F, 30.0F), 20, Random.Range(0.0F, 0.0F));
             Instantiate(astroid, position, Quaternion.identity);
             nextSpawnTime += spawnRate;
         }
 
     }
 }

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 aaronmhargrove · May 10, 2016 at 09:08 PM 0
Share

Okay, I tried both methods of the suggestions I got, both work. But my clones of the parent do not kill me (I'm using distance formula). Any ideas why?

avatar image JoshDangIt aaronmhargrove · May 11, 2016 at 03:25 AM 0
Share

Based on the other comment you made, I think you'll want to make a different script to put on the asteroid that kills the player from there ins$$anonymous$$d (It would be much better practice). Have you watched any guides on creating and instantiating prefabs?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

instanstiate Spawns more then one objec 0 Answers

Enemies spawn on top of each other(C#)(Unity) 1 Answer

Increasing spawn rate over time 1 Answer

Network spawning 1 Answer

AI spawning areas. What are the ways to make them? 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