Results 1 to 6 of 6
  1. #1
    Join Date
    May 2007
    Location
    UK
    Posts
    317

    How do I Instantiate a prefab every few seconds.

    I have an enemy that needs to instantiate a prefab every few seconds. What is the best way of creating a delay between the instantiation of these prefabs ? I'm using C# Thanks in advance I think I might need to use a coroutine...
    applelemonhead3345 (1 year ago) Show Hide

    best movie ever saw it first when i was two
    i think if jen was a real person she would be hot

    Trialbystone17 (11 months ago) Show Hide

    Jen was a guy dude.

  2. #2
    Join Date
    Jan 2011
    Posts
    102
    yield WaitForSeconds(amountOfTimeInSeconds)

    Is that what you're looking for?

    If it is, the function must be of type IEnumerator. You'll want to follow something similar to this, use this as reference:
    Code:
    http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html
    Last edited by jimmiduff21; 03-24-2011 at 10:46 AM.

  3. #3
    Join Date
    May 2007
    Location
    UK
    Posts
    317
    That's the winner! Thanks

    Code:
    	
    IEnumerator Start() 
    {
            while(gameObject)
    	{
    		SpawnBomb();
          	        yield return new WaitForSeconds(bombDelay);
    	}
    }
    applelemonhead3345 (1 year ago) Show Hide

    best movie ever saw it first when i was two
    i think if jen was a real person she would be hot

    Trialbystone17 (11 months ago) Show Hide

    Jen was a guy dude.

  4. #4
    Join Date
    Jan 2011
    Posts
    102
    You're welcome, glad I could help.

  5. #5
    Join Date
    May 2007
    Location
    UK
    Posts
    317
    I found an even simpler way of doing this using Invoke, just one line of code!

    Code:
    InvokeRepeating("SpawnBomb", 2.0f, 2.0f);
    applelemonhead3345 (1 year ago) Show Hide

    best movie ever saw it first when i was two
    i think if jen was a real person she would be hot

    Trialbystone17 (11 months ago) Show Hide

    Jen was a guy dude.

  6. #6
    Join Date
    Jan 2011
    Posts
    102
    Sweet, thanks for teaching me something new.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •