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 Voluntas · Jul 16, 2013 at 01:45 PM · spawnnot workingrespawn

Help! My respawn script isn't working! (Answered)

My respawn script isn't working and i'm new to scripting, although I have watched alot of tutorials, so i have a a very faint idea of what i'm doing. Here is my script:

 var Enemy : Transform;
 var Spawn : Transform;
 var RespawnTime : float = 60;
 var RespawnDelay : float = 10;
 
 function Start ()
 {
     // Spawn enemy with RespawnTime as interval.
     InvokeRepeating("SpawnEnemy", 0, RespawnTime);
 }
  
 function SpawnEnemy()
 {
    Instantiate(Enemy,Spawn.position,Spawn.rotation);
 }

The problem is respawning. It's supposed to spawn the enemy after a specific mount of time, but it simpily won't spawn after the first time. Can anybody offer any insight or expertiese as to why it's not working? Thanks for any replies in advance.

EDIT: thanks to everyone for their help and percistance! i've changed the script in the question to the one that works so that anyone in need of help in the future can use it. thanks again to everyone, and Chris Woo, who's script was the right one! :D

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 amphoterik · Jul 16, 2013 at 01:45 PM 0
Share

Please fix your code so that it is formatted correctly.

4 Replies

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

Answer by ChrisWoo · Jul 22, 2013 at 12:40 PM

This should work for you. You can't yield in Update.

 var Enemy : Transform;
 var Spawn : Transform;
 var RespawnTime : float = 60;
 var RespawnDelay : float = 10;
 
 function Start ()
 {
     // Spawn enemy with RespawnTime as interval.
     InvokeRepeating("SpawnEnemy", 0, RespawnTime);
 }
 
 function SpawnEnemy()
 {
    Instantiate(Enemy,Spawn.position,Spawn.rotation);
 }
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
2

Answer by amphoterik · Jul 16, 2013 at 01:54 PM

The problem is that you are only spawning the enemy when the script is first created (at the beginning of your game). You will also need a method to respawn the enemy after it dies. Basically, when the enemy dies, call this function (let's call is "RespawnAfterDelay") and let that recreate the enemy:

 function Start()
 {
     RespawnAfterDelay()
 }

 function RespawnAfterDelay()
 {
     yield WaitForSeconds (RespawnDelay);
     Instantiate(Enemy,Spawn.position,Spawn.rotation);
     RespawnAfterDelay();
 }

EDIT: fixed it to answer your question

Comment
Add comment · Show 14 · 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 Voluntas · Jul 16, 2013 at 09:34 PM 0
Share

this still isn't working, but i have incorperated it into the code in the question. thanks for the help, and the quick response

avatar image amphoterik · Jul 17, 2013 at 12:47 AM 0
Share

Are you calling the function when the enemy dies?

avatar image Voluntas · Jul 17, 2013 at 05:11 PM 0
Share

i'm after the script constantly spawning the enemies, and not after they are killed.

avatar image amphoterik · Jul 18, 2013 at 11:59 AM 0
Share

O$$anonymous$$, I fixed it to do what you want now. It is now a recursive function that keeps running. You may want to add checks to end it if need be

avatar image AlucardJay · Jul 22, 2013 at 12:27 PM 1
Share

Upvoted for persistance !

Show more comments
avatar image
0

Answer by inkspot · Jul 17, 2013 at 05:20 PM

Hello Voluntas!

What you need to do is call the function 'function RespawnAfterDelay()' after a period of time. for the effect you want. What you need to do is add something like a countdown(or countup) maybe this fast written down simple example will help you on your way to understand the concept;

 function Update() {
 if(RespawnDelay < 1) { /// check if the countdown reached 0
 RespawnAfterDelay(); ///calls your spawn function
 // reset your countdown e.g RespawnDelay = 60; //
 } else {
 RespawnDelay = RespawnDelay -1; /// removes 1 second from the timer
 }}
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 AlucardJay · Jul 26, 2013 at 11:37 PM 0
Share

/// removes 1 second from the timer

Actually, this removes 1 every frame. To make it time relative, use Time.deltaTime :

 RespawnDelay = RespawnDelay - Time.deltaTime;
avatar image
0

Answer by RyanZimmerman87 · Jul 26, 2013 at 11:29 PM

Hmm this will be in C# since I never used JavaScript.

 //make sure you set this to the right location
 Transform spawn;
 
 //timer for Ienumerator function
 float timer;
 
 //store your enemy prefab here
 public GameObject enemyObject;
 
 void Start()
 {
 timer = 60;
 
 //calls first function to spawn first enemy and start timer for next one
 firstEnemySpawnFunction();
 }
 
 void firstEnemySpawnFunction()
 {
 Instantiate(enemyObject, spawn.position, Quaternion.identity);
 
 StartCoroutine(spawnEnemyFunction(timer));
 }
 
 
 IEnumerator spawnEnemyFunction(float timer)
 {
 yield return new WaitForSeconds(timer);
 Instantiate(enemyObject, spawn.position, Quaternion.identity);
 
 //could adjust timer her if you want for example
 //timer = timer -1;
 
 StartCoroutine(spawnEnemyFunction(timer));
 }


I think that will work really easy for this kind of thing, you will just need to format it from C# to JavaScript (hopefully JavaScript has this functionality I'm not very familiar).

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

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

Help Needed With Object. 0 Answers

A respawn script for enemies. 1 Answer

How do I randomize the order of spawn points without repeats? 1 Answer

Respawning my AVATAR 1 Answer

the textures look bad 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