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 Elaine · Mar 11, 2011 at 04:53 AM · delay

How to delay a character from spawning

Hi everyone, I've been at this all day, but have not found an ideal solution. Please bear with me as I am new to Unity. I have made a simple game space shooter type game. I have one character that falls down the screen, and if it collides with the player, the player gets a life added to him. Right now, as soon as the extra life character falls below the camera view, he is automatically respawned at a random location on top and falls back down the scree. My question is, how can I delay the time between spawns of this "extra life" character? Right now, my javascript for the "extra life" character is:

var lifegermSpeed: int;

function Update () { amtToMove = lifegermSpeed * Time.deltaTime; transform.Translate(Vector3.down*amtToMove); if(transform.position.y <=-3){ transform.position.y = 20; transform.position.x = Random.Range(-6,6); }

}

Thanks in advance for any help with the issue!

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

4 Replies

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

Answer by Extrakun · Mar 11, 2011 at 06:53 AM

var lifegermSpeed: int; var canEnter : boolean;

function Update () { // prevent MoveAndSpawn() from being called if the 10 seconds timer is running if (canEnter) MoveAndSpawn(); }
}

function MoveAndSpawn() {

amtToMove = lifegermSpeed * Time.deltaTime; transform.Translate(Vector3.down*amtToMove);

if(transform.position.y <=-3){ transform.position.y = 20; transform.position.x = Random.Range(-6,6);
canEnter = false; yield WaitForSeconds(10.0);

}

// can enter this function again canEnter = true; }

You cannot use co-routine in Update(). Move your logic to a new function and call it from within Update(). However, as Update() is called every frame, you need a flag to prevent MoveAndSpawn() to be called again before the 10 seconds is done.

I would suggest that you separate the logic for moving and for re-spawning the life up,though.

Comment
Add comment · Show 4 · 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 Elaine · Mar 11, 2011 at 07:08 AM 0
Share

Thanks for your help everyone, especially Extrakun! I am now working on separating the logic for moving and re-spawning life. Also studying more about co-routines from the links provided. Your help was greatly appreciated :)

~Elaine

avatar image loopyllama · Mar 11, 2011 at 07:32 AM 0
Share

do not do this, it will start a new coroutine every frame. Under the hood, javascript will make $$anonymous$$oveAndSpawn return an IEnumerator. To fix this code you shouldmake yet another function that is called within the if statement. That function should set the value of canEnter and the yield keyword. In that case you can move the rest of the code directly into Update.

avatar image Extrakun · Mar 11, 2011 at 07:44 AM 0
Share

@harmless. Of course you're correct. Thanks for spotting my oversight. I have changed the code.

avatar image loopyllama · Mar 11, 2011 at 08:28 AM 0
Share

@Extrakun rock on! @Elaine use this code!

avatar image
0

Answer by Kona · Mar 11, 2011 at 06:08 AM

You could use WaitForSeconds. I recall it beeing used differently with js than from c# which I use so can't really make an example with your code but here's the unity api for it:

http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html

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
0

Answer by loopyllama · Mar 11, 2011 at 06:11 AM

use a simple timer or use a coroutine.

for a timer:

http://www.google.com/search?q=unity3d+timer&ie=UTF-8&oe=UTF-8&hl=en&client=safari

for yield/coroutine:

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

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
0

Answer by Amoldadu · Apr 07, 2017 at 03:02 PM

When you are spawning a character you should add this code into your GameController script:

 IEnumerator Spawn () {
         yield return new WaitForSeconds (4.0f);
         while (timeSpent > 0) 
         {
             Vector3 spawnPosition = new Vector3 (Random.Range (100, maxWidth),transform.position.y, 0.0f);
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate (rawan, spawnPosition, spawnRotation);
             yield return new WaitForSeconds (Random.Range (4.0f, 5.0f));
 
         }
 
         yield return new WaitForSeconds (1.0f);
         gameOverText.SetActive (true);
         yield return new WaitForSeconds (1.0f);
         restartButton.SetActive (true);
         yield return new WaitForSeconds (0.0f);
         mainMenuButton.SetActive (true);
         yield return new WaitForSeconds (0.0f);
         BackToLevelsButton.SetActive (true);
     }

IEnumerator Spawn () { yield return new WaitForSeconds (4.0f); while (timeSpent > 0) { Vector3 spawnPosition = new Vector3 (Random.Range (100, maxWidth),transform.position.y, 0.0f); Quaternion spawnRotation = Quaternion.identity; Instantiate (rawan, spawnPosition, spawnRotation); yield return new WaitForSeconds (Random.Range (4.0f, 5.0f));

     }

     
 }

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

1 Person is following this question.

avatar image

Related Questions

WaitForSeconds not working in OnTriggerEnter event 2 Answers

3,2,1 go! before gameplay starts 1 Answer

Problem with while statement. 1 Answer

RPC delay in specific circumstances 1 Answer

Delay Jump 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