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 SlyyGuyy · Mar 08, 2014 at 04:43 PM · javascriptspawncopyenemiescopies

Enemy spawn issues

Currently I'm attempting to spawn enemies based off of the 3 that I currently have, pictured below (pink, blue, red). I want them to spawn new clones 1 at a time over a period of time. I've already attempted at accomplishing this, but it doesn't seem to want to actually create the new enemies. What do I do? I'll post what I have so far for reference and see if I'm calling something wrong or if I'm doing it completely wrong.

 #pragma strict
 
 var pinkProjectile : Transform;
 var blueProjectile : Transform;
 var redProjectile : Transform;
 
 //var pinkCopy : Transform;
 //var blueCopy : Transform;
 //var redCopy : Transform;
 
 var enemy : Transform;
 var shootCoolDown : float; //Wait 1.5 seconds before shooting again
 var shootTimer : float; //Records how much time between shots
 
 //var enemies : GameObject[];
 
 var pinkLives = 1;
 var blueLives = 2;
 var redLives = 3;
 
 var spawnEnemies = 0;
 var spawn = false;
 
 var timesHit = 0;
 //var enemyArray = new Array();
 //enemyArray.length = 3;
 
 function Start () {
     if(spawn == false) {
         InvokeRepeating("spawnEnemy", 3, 5);
         spawn = true;
     }
 }
 
 function Update () {
     shootLazer();
 }
 
 function OnTriggerEnter(collision:Collider) {
     if(gameObject.name == "Enemy_Pink") {
         if(collision.gameObject.tag == "Bullet") {
             pinkLives--;
             if(pinkLives == 0) {
                 Destroy(GameObject.Find("Enemy_Pink"));
             }
         }
     }
     if(gameObject.name == "Enemy_Blue") {
         if(collision.gameObject.tag == "Bullet") {
             blueLives--;
             if(blueLives == 0) {
                 Destroy(GameObject.Find("Enemy_Blue"));
             }
         }
     }
     if(gameObject.name == "Enemy_Red") {
         if(collision.gameObject.tag == "Bullet") {
             redLives--;
             if(redLives == 0) {
                 Destroy(GameObject.Find("Enemy_Red"));
             }
         }
     }
     Destroy(collision.gameObject);
 }
 
 function spawnEnemy() {
     var instantiatedEnemy : Transform;
     var newEnemyOriginX = enemy.transform.position.x;
     var newEnemyOriginY = enemy.transform.position.y;
     var newEnemyOriginZ = enemy.transform.position.z;
     instantiatedEnemy = Instantiate(enemy, Vector3(newEnemyOriginX, newEnemyOriginY, newEnemyOriginZ), transform.rotation);
     instantiatedEnemy.rigidbody.AddForce(instantiatedEnemy.transform.up * - 20);
     spawnEnemies++;
 }

alt text

screen shot 2014-03-08 at 10.41.36 am.png (91.2 kB)
Comment
Add comment · Show 3
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 fifthknotch · Mar 08, 2014 at 06:39 PM 0
Share

Do you $$anonymous$$d removing all irrelevant code.

avatar image suribe · Mar 08, 2014 at 06:51 PM 0
Share

Are you getting some error in the console? Is it spawning any enemy at all, or none?

avatar image SlyyGuyy · Mar 08, 2014 at 11:34 PM 0
Share

All irrelevant code has been removed here: var enemy : Transform;

 function Start () {
     if(spawn == false) {
         InvokeRepeating("spawnEnemy", 3, 5);
         spawn = true;
     }
 }
 
 function spawnEnemy() {
     var instantiatedEnemy : Transform;
     var newEnemyOriginX = enemy.transform.position.x;
     var newEnemyOriginY = enemy.transform.position.y;
     var newEnemyOriginZ = enemy.transform.position.z;
     instantiatedEnemy = Instantiate(enemy, Vector3(newEnemyOriginX, newEnemyOriginY, newEnemyOriginZ), transform.rotation);
     instantiatedEnemy.rigidbody.AddForce(instantiatedEnemy.transform.up * - 20);
     spawnEnemies++;
 }

And to answer your questions, I'm getting no errors in the console and it's spawning 0 enemies

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by suribe · Mar 08, 2014 at 11:46 PM

If with that code it's still not working, then spawn must be true for some reason. If you are using it only because you fear it might be called more than once, that should not happen if the script is used only in one gameObject in the scene. If you are using it in more than one, then you should redesign your game so this is centralized.

I would suggest adding a couple Debug lines in the Start function:

 function Start () {
     Debug.Log("spawn = " + spawn);
     if(spawn == false) {
        Debug.Log("Before InvokeRepeat");
        InvokeRepeating("spawnEnemy", 3, 5);
        Debug.Log("After InvokeRepeat");
        spawn = true;
     }
 }

And also in spawnEnemy:

 function spawnEnemy() {
     var instantiatedEnemy : Transform;
     var newEnemyOriginX = enemy.transform.position.x;
     var newEnemyOriginY = enemy.transform.position.y;
     var newEnemyOriginZ = enemy.transform.position.z;
     Debug.Log("spawnEnemy");
     instantiatedEnemy = Instantiate(enemy, Vector3(newEnemyOriginX, newEnemyOriginY, newEnemyOriginZ), transform.rotation);
     instantiatedEnemy.rigidbody.AddForce(instantiatedEnemy.transform.up * - 20);
     spawnEnemies++;
     Debug.Log("spawnEnemies: " + spawnEnemies);
     Debug.Log("Instantiated Enemy: " + instantiatedEnemy );
 }

Or directly, try replacing your Start function with something that only invokes the repeating:

 function Start () {
      InvokeRepeating("spawnEnemy", 3, 5);
 }
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

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

22 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

Related Questions

Stop spawning enemies when a bool is enabled 0 Answers

Spawn random amount apart of each other 1 Answer

Enemy spawn script not working properly ! 1 Answer

How to spawn enemies/objects that fly into a level 1 Answer

character moving after moving 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