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 samvilm · Mar 11 at 12:47 AM · gameobjectarraysgameobjectsarray of gameobjectsgameobject.find

Find one inactive player (gameobject)

when my player gets a power up i want it to multiply x2. The way I have it set up (tell me if there is a better way) is that I have a bout 10 players at start but only one is active. When the player gets the power up I want to activate one of the inactive players. The player can keep getting x2 power ups creating numerous players on the scene. My biggest problem is that its proving hard to find just one inactive player and activate it. All the players are children of 1 parent so I thought of using getcomponentinchildren(true) but that could return an already active player. I assume I should make an array of all the players at start with getcomponentsinchildren and then somehow each time the player hits the power up go through the array looking for 1 inactive player to activate. I have already read all the other posts about this and am still stuck. The code I have so far to create the array is this but it doesn't seem to work. Any help would really be appreciated!

 Transform[] allChildren;
   public GameObject playerparent;
 
  void Start()
 {
  allChildren = playerparent.GetComponentsInChildren<Transform>(true);
              foreach (Transform child in allChildren)
           {
               child.gameObject.SetActive(false);
           }
 }
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 Caeser_21 · Mar 13 at 10:28 AM 0
Share

You could do :

  Transform[] allChildren;
    public GameObject playerparent;
  
   void Start()
  {
        allChildren = playerparent.GetComponentsInChildren<Transform>(true);
        foreach (Transform child in allChildren)
       {
          if (child.gameObject.ActiveSelf == false)
          {
               child.gameObject.SetActive(true);
           }
       }
 }
avatar image samvilm Caeser_21 · Mar 15 at 12:19 AM 0
Share

Thanks but I tried this but it doesn't work. no error, just nothing happened. This is the script i used:

 foreach (Transform child in GameControll.instance.allChildren)
              { if (child.gameObject.activeSelf == false)
           {child.gameObject.transform.position = new Vector2(gameObject.transform.position.x+2, gameObject.transform.position.y);
 
               child.gameObject.SetActive(true);
           }
              }

in start of gamecontroll i have

             allChildren = playerparent.GetComponentsInChildren<Transform>(true);
 

2 Replies

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

Answer by Caeser_21 · Mar 15 at 03:27 AM

Ok, so I think i found an answer :

 foreach (Transform child in GameControll.instance.allChildren)
 { 
      if (child.gameObject.activeInHierarchy == false)
      { 
           child.gameObject.transform.position = new Vector2(gameObject.transform.position.x+2, gameObject.transform.position.y);
  
                child.gameObject.SetActive(true);
                break;
       }
 }
Comment
Add comment · Show 3 · 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 samvilm · Mar 16 at 12:41 AM 0
Share

great! works like a charm! thanks!

avatar image samvilm · Mar 22 at 12:30 AM 0
Share

Just a small side problem; when i start on the game scene with the players my script doesn't collect the array.. it only makes it if I come from the main menu. (which is fine for actual gameplay but annoying for testing). any idea why?

avatar image Caeser_21 samvilm · Mar 22 at 04:06 AM 0
Share

It might be because you only call the part of the script that collects the array once you press the main menu button...
But just in case can you post the GameController and the MainMenu code

avatar image
0

Answer by aqeel25 · Mar 11 at 04:25 AM

 public GameObject playerparent;
 for (int j = 0; j < playerparent.gameObject.transform.childCount; j++)
         {
 
  playerparent.transform.GetChild(j).gameObject.SetActive(True);
           }

Try This

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 samvilm · Mar 12 at 11:21 PM 0
Share

How will this only pick up an inactive player? Wont it just choose a player whether active or not?

avatar image aqeel25 samvilm · Mar 14 at 04:25 AM 0
Share

You want to activate the inactive player and this will do it for u making all Active that includes Inactive

avatar image samvilm aqeel25 · Mar 15 at 12:12 AM 0
Share

But i want to do it one at a time, each time the player gets a power up?

Show more comments

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

201 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 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

Three Spots For Three Random Objects 1 Answer

How to assign a gameobject to a script that is added dynamically 1 Answer

GameObjects are Pink 1 Answer

Cannot place checkpoints position into Transform array 1 Answer

JavaScript 3 Arrays questions 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