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 Digital-Phantom · Jan 09, 2015 at 02:13 PM · spawntimerwaitforsecondsdelay

How do I put a delay in this script ?

I have a fairly simple script that spawns weapons at a choice of locations. Currently it spawns a new weapon the exact moment the old one is collected. How do I go about putting a delay on that?

I thought a simple 'yield WaitForSeconds()' statement would do it, but you having no luck. I've tried adding the waitforseconds before/after almost every line in the script but it either has no effect at all, the weapon spawn goes wild or I get error messages about functions can't be coroutines?

 #pragma strict
 
 var spawnPoint : Transform[]; // Create list of possible spawn locations
 var wepType : GameObject[]; // Creat list of possible weapons to be spawned
 var wepPresent : boolean = false;
 
 function Start()
 {
     SpawnWeapons(); //Run SpawnWeapons coroutine
 }
 
 function Update()
 {    
      if(wepPresent == false) //Check to see if weapon is present
        {
         SpawnWeapons(); //If no weapon is present run SpawnWeapons coroutine
        }
 }
 
 function SpawnWeapons() //Spawns a random weapon at one of possible spawn locations
 {
     var randomLoc : int = Random.Range(0,spawnPoint.length); //Pick random number between the min and max values of the array containing the spawn locations.
     var randomWep : int = Random.Range(0,wepType.length); //Pick random number between the min and max of the array containing the weapon types.
     var newWeapon = Instantiate(wepType[randomWep], spawnPoint[randomLoc].position, spawnPoint[randomLoc].rotation);
     
     newWeapon.GetComponent(DestroySelfOnContact).getSpawnHandler = this.gameObject;
     //Pick random weapon depending on which number was generated by 'randomWep'
     //Spawn weapon at location depending on which number was generated by 'randomLoc'
 
     wepPresent = true;
     
     if(newWeapon.GetComponent(DestroySelfOnContact).destroyed == true)
     {
         wepPresent = false;
     }
 }

Any suggestions please guys ???

Comment
Add comment · Show 2
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 Digital-Phantom · Jan 09, 2015 at 02:31 PM 0
Share

Sorry, what exactly does that mean/do ?

And also that gives me the following error -

Assets/Scripts/RandomWeaponRandomLocation.js(15,35): BCE0005: $$anonymous$$ identifier: 'frame'.

???

avatar image ExtremePowers · Jan 09, 2015 at 02:37 PM 1
Share
 private var frame : int;
 var DelayInFrames : int = 2500;
 
 function Update() {
     frame++;
     if (!wepPresent && frame % DelayInFrames == 0) { //This will check if remainder of frame divided by DelayInFrames is 0 (If the frame number is 2500 dividable by DelayInframes and it is a round number)
         SpawnWeapons();
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GameVortex · Jan 09, 2015 at 02:30 PM

You can supply the delay to the SpawnWeapons function and start a WaitForSeconds coroutine: Example:

 function SpawnWeapons(delay : float)
 {
     wepPresent = true; //Immediately change so the SpawnWeapons will not be constantly called by Update
     yield WaitForSeconds(delay);

     //Do weapon spawning here
 }
Comment
Add comment · Show 3 · 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 Digital-Phantom · Jan 09, 2015 at 02:55 PM 0
Share

getting errors -

Assets/Scripts/RandomWeaponRandomLocation.js(21,23): BCE0043: Unexpected token: var.

Assets/Scripts/RandomWeaponRandomLocation.js(21,27): BCE0044: expecting EOF, found 'delay'.

???

avatar image GameVortex · Jan 09, 2015 at 03:03 PM 1
Share

Sorry about that, I don't usually work in UnityScript. But as a recommendation: ins$$anonymous$$d of being stopped by every single little compile error you get you should search around and try to figure out what the issue could be and learn from it. Especially when it is about something as basic as parameters for a function.

The Scripting tutorials by unity is good place to start: =) http://unity3d.com/learn/tutorials/modules/beginner/scripting/variables-and-functions

Anyway, I edited my answer to be more correct.

avatar image Digital-Phantom · Jan 09, 2015 at 03:15 PM 0
Share

Turns out

 function SpawnWeapons()
 {
 
 wepPresent = true; //Immediately change so the SpawnWeapons will not be constantly called by Update
 yield WaitForSeconds(delay);
 
 }

is actually what I needed. Didn't like having anything in the function brackets.

The change I made to your line, plus declaring my var at start of script and now works fine.

Wouldn't have thought of it though without your help, so many thanks !

:)

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Better way for a timer to load a level? 1 Answer

C# simple delay execution without coroutine? 2 Answers

Speed does not work? 1 Answer

Help With Initiating Bomb Every Specific Second 1 Answer

Increasing spawn rate over time 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