- Home /
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);
}
}
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);
}
}
}
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);
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;
}
}
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?
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
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
How will this only pick up an inactive player? Wont it just choose a player whether active or not?
You want to activate the inactive player and this will do it for u making all Active that includes Inactive
But i want to do it one at a time, each time the player gets a power up?
Your answer
Follow this Question
Related Questions
Three Spots For Three Random Objects 1 Answer
Cannot place checkpoints position into Transform array 1 Answer
JavaScript 3 Arrays questions 1 Answer
How do I conditionally instantiate gameobject in a public class array? 0 Answers
How to assign a gameobject to a script that is added dynamically 1 Answer