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 justaskin324 · May 18, 2014 at 06:09 AM · javascriptspawn

Spawning script error

So i need to make a spawner that spawns at a random location given. If there's anything wrong with this code tell me and help me fix it.

 #pragma strict
 
 var spawnObject : GameObject;
 var spawnPoint : GameObject;
 var spawnCounter : int= 0;
 var spawnCounterMax : int= 0;
 
 function Update () {
 
 this.spawnCounter++;
     
 if (this.spawnCounter >= this.spawnCounterMax){
     Instantiate(this.spawnObject, this.spawnPoint.transform.position.x + Random.Range(-2,2),
                                   this.spawnPoint.transform.position.y + Random.Range(6.23,6.3),
                                   this.spawnPoint.transform.position.z + Random.Range(-10,-10.1),
                                   this.spawnPoint.transform.rotation);
 
     this.spawnCounter = 0; 
Comment
Add comment · Show 6
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 robertbu · May 18, 2014 at 06:57 AM 0
Share

What is it doing or not doing that you need it to do? One issue is how you are "ti$$anonymous$$g" your spawning. Frames don't come at perfectly even rate, and the fps will vary greatly from device to device. Look up answers with 'timer' or 'timestamp'.

avatar image justaskin324 · May 18, 2014 at 06:40 PM 0
Share

can you edit this script for me?

avatar image Jeff-Kesselman · May 18, 2014 at 06:45 PM 0
Share

NO. Answers is not the place to beg for people to write scripts for you.

If you have specific questions, people are happy to give specific answers.

Otherwise, i suggest you look under collaboration and work for hire in the forums.

avatar image MrFijiWiji · May 18, 2014 at 06:53 PM 0
Share

Well for one, you're only going to spawn anything if the spawn counter is greater than the spawnCounter$$anonymous$$ax, did you mean to say <= (lower than or equal to) as opposed to >= (greater than or equal to)?

Also, why are you setting spawn counter to 0 after each instantiation? That's going to cause you to enter an endless loop. $$anonymous$$oving the spawnCounter ++ into the if block of code would make far more sense. That would increase it after every instantiation. Once you reach your max, you can then set the spawnCounter to 0.

In my opinion, this entire section of code should be moved out of update and into a dedicated spawn function that you can call when you need it.

avatar image robertbu · May 19, 2014 at 12:46 AM 0
Share

Since spawnCounter counts frames, I imagine you have some specific time you want to way between spawns. One way to hadle this issue is to use InvokeRepeating(). Another is a coroutine, and a third is to use a timestamp within update. If you search for these three topics, you will find many posts.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

touch input certain screen part 1 Answer

Rotation(Y) and Acceleration Camera 0 Answers


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