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
1
Question by Persona · Jul 31, 2010 at 02:15 PM · respawnitem

Random Spawning Question

I've been working on a random spawning script for power ups in a side scrolling shooter game but for some reason the spawner always fires it at the same time and not randomly, may I ask if anyone can see a problem with this script?

//What power up are we spawning? var PowerUp: GameObject; //Minimum alloted time var minTime: float = 1.0; //Maximum allotted time var maxTime: float = 10.0;

// function Update () { //Is the item already Out? if (GameObject.FindWithTag("PowerUp") == false){ //Wait for random time before respawning WaitForSeconds(Random.Range(minTime, maxTime)); //Set random position inside X&Y axis and materialize var position = Vector2(Random.Range(0.01, 0.5), Random.Range(0.10, 0.90)); Instantiate(PowerUp, position, Quaternion.identity); }

}

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

2 Replies

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

Answer by Mike 3 · Jul 31, 2010 at 02:28 PM

You need to use yield when you're using WaitForSeconds, and you can't do it from Update - Update can't be a coroutine. On top of that, you'd need to add a lock to prevent the coroutine being called multiple times and spawning loads of them.

Also, use == null instead of == false for your object checks - even if false works, it just looks odd

//What power up are we spawning? var PowerUp: GameObject; //Minimum alloted time var minTime: float = 1.0; //Maximum allotted time var maxTime: float = 10.0;

var spawning = false;

function Update () { //Is the item already Out? if (!spawning && GameObject.FindWithTag("PowerUp") == null){ SpawnObject(); } }

function SpawnObject() { spawning = true;

 //Wait for random time before respawning
 yield WaitForSeconds(Random.Range(minTime, maxTime));
 //Set random position inside X&Y axis and materialize
 var position = Vector2(Random.Range(0.01, 0.5), Random.Range(0.10, 0.90));
 Instantiate(PowerUp, position, Quaternion.identity);
 spawning = false;

}

Comment
Add comment · Show 5 · 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 e-bonneville · Jul 31, 2010 at 02:31 PM 0
Share

@$$anonymous$$ike why do you always have to one-up me? I was here first... Just kidding. ;)

avatar image Mike 3 · Jul 31, 2010 at 02:32 PM 0
Share

$$anonymous$$uahahahahahaha

avatar image e-bonneville · Jul 31, 2010 at 02:34 PM 0
Share

@$$anonymous$$ike - lol.... I hate this 15 character limit. I can't just write 'lol'.

avatar image Persona · Jul 31, 2010 at 05:40 PM 0
Share

Thank you both!

avatar image zac 1 · Mar 25, 2011 at 02:43 AM 0
Share

Thanks lot.. it helps me out...

avatar image
0

Answer by e-bonneville · Jul 31, 2010 at 02:24 PM

Your script is fine, as far as I can see. I might have a solution, though. I don't know if this is your problem, but it is something that happens a lot with me. If you create a variable within your script, and then change it later on, it remains the same wherever the script is instantiated. For example, if you created minTime and maxTime as the same value, and then changed the value in the script, it would remain the same in the inspector. So just check the inspector and see if both time values are correct.

Also, I just noticed this on re-reading your script, you'll want to use yield WaitForSeconds instead of just WaitForSeconds. One more thing - it's always a good idea to space your code correctly. Instead of having

function Update() {
if (!isGoodFormatting) {
print ("This is not good formatting.");
}
}

You should have

function Update() {
    if (isGoodFormatting) {
        print("This is good formatting");
    }
}
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

No one has followed this question yet.

Related Questions

Reset GUI texture after respawn 1 Answer

Random Player Respawn Points 3 Answers

Respawn Code Null Reference Exception 1 Answer

Checkpoint system??? 3 Answers

die then respawn after couple of secs when touching a ridgid body? 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