0
\$\begingroup\$

I'm developing a bowling game in which I'm currently working on the winner loose scene. I have a parent gameobject which contains two character models having the same gameobject tags. For the two player game I'm using the same tag for both the players for their individual turns. Now as in the end of the game I have to display both the characters at the same time having to play the winner and the defeat animation using the same tag. How can this be done or is there any other way this can be done?

To obtain all the child characters from parent tag, this is what I've tried at runtime:

modelcontainer = GameObject.FindGameObjectWithTag("modelcontainer");

        findgameobjects = new GameObject[modelcontainer.transform.childCount];

        for (int i = 0; i < modelcontainer.transform.childCount; i++)
        {
            findgameobjects[i] = modelcontainer.transform.GetChild(i).gameObject;
        }

But it doesn't store the child gameobject which is not active at current time as per my game logic.

\$\endgroup\$
  • \$\begingroup\$ You know you're not limited to searching for gameobjects by tag, right? You can store a reference to player 1 in one variable and player 2 in another, or use a player ID script on each one to track their player identity, etc. What alternative methods have you already tried? \$\endgroup\$ – DMGregory Jul 20 '17 at 16:31
  • \$\begingroup\$ I might get individual characters referenced individually to variables, but the way im using characters in the game with same tag is, making one character set active to true and the other to false based upon their turn. Now in the case of ending of the game, how would i reference the character which is not active. I've tried using activeinhierarchy and isactive but both didn't work for me. \$\endgroup\$ – Salar Muhammad Jul 20 '17 at 16:41
  • \$\begingroup\$ It sounds like you should show us a minimum, complete, verifiable example of your code so that we can reproduce the problem and help you debug it. \$\endgroup\$ – DMGregory Jul 20 '17 at 16:56
  • \$\begingroup\$ Please see my edited question \$\endgroup\$ – Salar Muhammad Jul 20 '17 at 17:02
1
\$\begingroup\$

Found a way to display both characters having same gameobject tag at the same time.

 modelcontainer = GameObject.FindGameObjectWithTag("modelcontainer");

        findgameobjects = new GameObject[modelcontainer.transform.childCount];

        for (int i = 0; i < modelcontainer.transform.childCount; i++)
        {
            findgameobjects[i] = modelcontainer.transform.GetChild(i).gameObject;
            if(i==0)
            {
                firstcharacter = findgameobjects[i];
            }
            else
            {
                secondcharacter = findgameobjects[i];
                if(secondcharacter.activeInHierarchy==false)
                {
                    secondcharacter.SetActive(true);
                }
            }
        }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.