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 naqvir · Apr 18, 2012 at 03:18 AM · gameobjectinstantiategetcomponentfindrespawn

Limiting respawns on scene

Hi there

I just wanted to know what is the cheapest way of instantiating prefabs at spawn points, whilst keeping control of how many objects are in the scene. At the moment i have this, but i think it might be too computationally expensive. :s Basically i want to spawn enemy soldiers at random points of the map, but i want to control limit it to soldierMax.

 #pragma strict
 
 var prefab:GameObject; 
 var soldierMax:int;
 private var soldierCount:int; 
 private var respawnPoints:GameObject[];
 private var maxSpawnDelay:int;
 
 function Start () {
      respawnPoints = GameObject.FindGameObjectsWithTag("SoldierSpawn");    
 }
 
 function Update () {
     if(soldierCount < soldierMax) {
         respawnSoldier();        
     }
     else {
         soldierCount = GameObject.FindGameObjectsWithTag("Soldier").length;
     }        
 }
 
 function respawnSoldier() {
     while (soldierCount < soldierMax) {
         Instantiate(prefab,respawnPoints[Random.Range(0,respawnPoints.Length)].transform.position,Quaternion.identity);
         yield WaitForSeconds(Random.Range(0,maxSpawnDelay));
     }
 }

Would appreciate the guidance :)

Thanks 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 syclamoth · Apr 18, 2012 at 03:21 AM 2
Share

Just have some central counter object, that increments an integer every time you spwan something! That way you dont' need to keep making calls to 'GindGameObjectsWithTag', which is as you say quite expensive. Decrement the same counter every time something gets destroyed using the OnDestroy function to guarantee success.

avatar image naqvir · Apr 18, 2012 at 09:24 PM 0
Share

Im getting a strange error randomly when they spawn, what is "transform.position assign attempt for 'spawnSoldier(Clone)' is not valid. Input position is {NaN, 0.262370.NaN}" i have no idea how to fix this :S

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bodec · Apr 18, 2012 at 03:56 AM

Looks pretty good just as is, only part im not seeing reason behind is the else statement.I assume you are handling the destruction of the prefab else were in the script or you just don't want any more than max count to spawn. if you want a constant number up then you need to add a line decreasing you soldier count by one so your update picks up your one short.

Comment
Add comment · Show 2 · 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 naqvir · Apr 18, 2012 at 04:59 AM 0
Share

yup removed the else. Im getting a strange error randomly when they spawn, what is "transform.position assign attempt for 'spawnSoldier(Clone)' is not valid. Input position is {NaN, 0.262370.NaN}" i have no idea how to fix this :S

avatar image bodec · Apr 18, 2012 at 02:27 PM 0
Share

not sure hopefully some people with more experance in scriptting log on soon and may know the answer

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers

Get components of multiple instantiated objects? 2 Answers

GetComponent vs AddComponent 3 Answers

Instantiated Prefab using its transform and object on other scripts 1 Answer

Getcomponent error? 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