Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 chouchene4561 · Sep 13, 2021 at 12:14 PM · weaponweapon systemtransform.parent

ChildCount without counting the children of the child ?

i'm making a drop pick up program and i need to make a limit like u cant pick up if u have a child in the weapon holder and u can when there's no children the issue is my weapon object has children as game objects for the shooting script muzzflashes and trail effect stuff like that i need to calculate the child of the weapon holder without counting the weapon's children is it possible thanks

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
0
Best Answer

Answer by FlaSh-G · Sep 13, 2021 at 12:36 PM

I don't really understand the context, but you can iterate over a Transform to have all its children:

 foreach (Transform child in transform)

So you can just count yourself:

 var childCount = 0;
 foreach (Transform child in transform)
 {
     childCount++;
 }

With this, you can implement whatever you want.

That being said, I also just checked Transform.childCount in 2020.3.17, and it only counts direct children of an object anyway, children of children are ignored.

Comment
Add comment · Show 4 · 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 chouchene4561 · Sep 13, 2021 at 01:04 PM 0
Share

what if i use it with a tag ? can this work ?

  for (int i = 0; i < t.childCount; i++) 
          {
              if(t.GetChild(i).gameObject.tag == tag)
              {
                  return t.GetChild(i).gameObject;
              }

avatar image FlaSh-G chouchene4561 · Sep 13, 2021 at 01:06 PM 0
Share

Sure, why not! It would work with a foreach loop as well.

 foreach (Transform child in transform)
 {
     if (child.gameObject.CompareTag(tag))
     {
         return child.gameObject;
     }
 }
avatar image chouchene4561 · Sep 13, 2021 at 03:39 PM 0
Share

it works but i really need the childcount to be 0 or 1 cuz if its not the player can pickup 2 weapons which is not ideal is that posible to force it to be 0 or 1 ? if (transform.parent == holder && holder.childCount ==1 ) { Pickedup = true;

             }
              if (transform.parent != holder  && holder.childCount == 0)
             {
                 Pickedup = false;
             }
              else if (holder.childCount > 1)
         {
            
         }
         print(holder.childCount);
avatar image FlaSh-G chouchene4561 · Sep 13, 2021 at 05:27 PM 0
Share

No, you cannot force a count. Your code is going in the right direction here; check whether a child already exists (`childCount > 0`) and if it does, decide what to do next. Either you refuse to pick up another item or destroy the current one.

avatar image
0

Answer by logicandchaos · Sep 13, 2021 at 01:09 PM

If you there is a script on the components you want to get, you can use GetComponentsInChildren()

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

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

Related Questions

Best way to create a flamethrower weapon in 2d? 1 Answer

How do I stop my weapon from firing while reloading? 2 Answers

Can't set an int from seperate script 1 Answer

Reload with keypress work bad! 0 Answers

Weapon shooting mechanism problem. 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