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 JayFitz91 · Nov 20, 2013 at 12:58 PM · collisionenemyspawn

Trying to spawn enemies

This is the code i have my enemies, what i want to do is spawn another enemy when i kill the original, this works to an extent, only it stops the enemy completely and the enemy wont go after the player. But as well as that, it seems to keep cloning the enemies everytime i hit one without destroying the other. My hierarchy just looks like enemy(Clone)(Clone)(Clone) etc.

 var enemy : GameObject;
 
 var seenDistance = 40;
 var rotationSpeed = 100;
 var moveSpeed = 5;
 
 var nextSpawn : float = 0.0f;
 var spawnTime : float = 5.0f;
 
 function Start () 
 {
     nextSpawn = Time.time + spawnTime;
     
 }
 
 function Update () 
 {
     
     var distance = Vector3.Distance(transform.position, player.transform.position);
     
     if(distance < seenDistance)
     {
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.transform.position - transform.position),rotationSpeed * Time.deltaTime);
         transform.position += transform.forward * moveSpeed * Time.deltaTime;
     }
 
 }
 
 
 function spawnEnemy()
 {
     Instantiate(enemy, transform.position, Quaternion.identity);
 }
 
 function OnCollisionEnter(theCollision : Collision) 
 {
 
     if(theCollision.gameObject.tag == "bullet")
     {
         Destroy(theCollision.gameObject);
         
         if(Time.time > nextSpawn)
         {
             nextSpawn = Time.time + spawnTime;
             spawnEnemy();
         }
     }
 
 }


Below is the destroy code i have for my enemy which is in my bulletCollide script:

 function OnCollisionEnter(collision : Collision) 
 {
     Destroy (gameObject);
     Debug.Log("Hit");
     
     if (collision.gameObject.name == "Test" ||collision.gameObject.name == "StupidEnemy")
     {
         Destroy(collision.gameObject);
         Inventory.inventory[2] += 10;
     }
 }

Can someone tell me what it is im doing wrong? Any help would be appreciated.

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 gajdot · Nov 20, 2013 at 01:08 PM 0
Share

the clone of clone can be caused because you didn't linked the object to a prefab just a gameObject in your hierarhy, so it works till you kill the original?

To fix this just create a prefab from your enemy, by draging it to the project folder and then drag the one in the project folder onto your script.

avatar image sspross · Nov 20, 2013 at 01:56 PM 0
Share

yes a agree with gajdot, just make your enemy variable public and then drag the prefab on it in the inspector

avatar image JayFitz91 · Nov 21, 2013 at 04:33 PM 0
Share

I've made it public and the enemy was a prefab so unfortunately it didn't fix my problem, thanks for the response though.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xelareip · Nov 21, 2013 at 05:03 PM

If this script is on the prefab, that means the script is referencing its own GameObject. Because all links intern to a prefab stay intern to the GameObject when you create an instance, whenever you instantiate a new enemy, the variable in your script won't reference the prefab but the current GameObject.

To fix that you need the variable referenced by var enemy : GameObject to be located on a script that is not part of the prefab.

Not sure that I'm clear. Shorter version : If the script that spawns the enemy is attached to the prefab, you're gonna spawn a clone of the current object every time instead of a new instance of the prefab.

I'd recommend having a EnemySpawner script in your scene, reference you prefab in that script and spawn the enemies from there.

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 JayFitz91 · Nov 21, 2013 at 08:27 PM 0
Share

Ok thanks Xelareip, im still new enough to Unity but ill give it a go and let you know how i get on, thanks for the reply :)

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

20 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

Related Questions

Error: Instantiated Enemies don't get hit 2 Answers

Spawner continuing after boss dies 1 Answer

Trigger Spawning? 1 Answer

How do I go about adding basic pathfinding to my enemy? 1 Answer

Enemy death help 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