• 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 ciwiaf · Sep 29, 2015 at 11:07 AM · childspawningchild-to-gameobject

transform.childCount question

This is probably very easy to solve, but I'm stuck right now. I'm trying to make 1 mole spawn to a hole. I have this code below where if the childcount is 0, it should spawn 1 mole, after instantiating the mole, it'll be assigned as a child of the hole. But it just keeps on spawning, I want it to stop after spawning 1 and wait for it to disappear before spawning a new one.. am I using the transform.childCount wrong?

 void Update(){
 
         if (transform.childCount == 0) {
 
             Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
 
         }
 
         Debug.Log (transform.childCount);
 
     }
     
     void Spawn(){
         
         //Instantiate mole in the hole
         GameObject gMole = (GameObject)Instantiate(mole, transform.position, Quaternion.identity);
         gMole.transform.parent = transform;
         
     }

Comment
Add comment · Show 4
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 Umresh · Sep 29, 2015 at 12:24 PM 1
Share

Quick fix is adding cancel invoke

     void Spawn(){
              
              //Instantiate mole in the hole
              GameObject g$$anonymous$$ole = (GameObject)Instantiate(mole, transform.position, Quaternion.identity);
              g$$anonymous$$ole.transform.parent = transform;
              CancelInvoke("Spawn");
          }
avatar image ciwiaf Umresh · Sep 29, 2015 at 01:19 PM 0
Share

this works great!

avatar image brentshermana · Dec 30, 2015 at 07:37 PM 0
Share

$$anonymous$$any solutions have been offered, but I don't see any explanation regarding why there's a problem in the first place. I'm having similar troubles with childCount, so that would be much appreciated.

avatar image Dave-Carlile brentshermana · Dec 30, 2015 at 07:58 PM 1
Share

The second parameter on the Invoke function is the time to wait before calling the invoked function. Say you pass in 1 second. The Update method continues to be called every frame, and childCount continues to be 0 until that original 1 second has passed. If Update is being called at 60fps then Invoke gets executed 60 times before the first Spawn is ever invoked and childCount becomes > 0.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Umresh · Sep 29, 2015 at 12:24 PM

In your case instantiating takes time(i.e. invoking spawn) it is called until the object is spawned and child count is not 0. You'll have to check it manually if spawn is called or not.

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

Answer by Dave-Carlile · Sep 29, 2015 at 01:56 PM

Another solution is to use a Coroutine as your spawner...

 void Start()
 {
   StartCoroutine(Spawner());
 }


 IEnumerator Spawner()
 {
   while (true)
   {
      if (transform.Count == 0)
      {
        // wait a bit - this is what Invoke was doing
        yield return WaitForSeconds(Random.Range(spawnMin, spawnMax));

        // your original Spawn method, without the CancelInvoke
        Spawn();  
      }

      // all done for this frame
      yield return null;
   }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Adding child to a instantiated prefab. 0 Answers

Why gameObj's pos changes when a child is added ? 2 Answers

UNET Clients spawn Childs on Player & Client Sync problems. 0 Answers

Why is parenting not working? 0 Answers

Get Child from a parent 3 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