- Home /
gameObject.setActive not working even though function gets triggered
I'm using the new Unity 4.6 UI to show a player row for each player that is in the game. I'm using gameObject.setActive to hide/show the row. These rows are in a gameobject that initially gets disabled and is enabled when the game is started(there are enough players). For some reason the row gameobject is not reactivated with setActive even though the function that sets the gameobject active is called.
This is in my script that handles the player row:
private bool m_Taken = false;
public bool Taken
{
get
{
return m_Taken;
}
}
public void Take(PhotonPlayer player)
{
if (m_Taken)
return;
m_Taken = true;
/* this part is called by ActivateRow but the gameObject stays inactive */
gameObject.SetActive(true);
}
void Start()
{
Untake();
}
public void Untake()
{
m_Taken = false;
gameObject.SetActive(false);
}
And this handles the activation of a row for a player (PartnerMatchingPage):
private PartnerRowHandler[] selectablePlayerRows;
private Dictionary<int, PartnerRowHandler> playerRows;
public PartnerRowHandler ActivateRow(PhotonPlayer player)
{
int ID = player.ID;
/* checks whether the player doesn't already have a row */
if (playerRows.ContainsKey(ID))
return playerRows[ID];
for (int i = 0; i < selectablePlayerRows.Length; i++)
{
if (selectablePlayerRows[i].Taken)
continue;
/* assign found inactive row to the given player */
selectablePlayerRows[i].Take(player);
playerRows[ID] = selectablePlayerRows[i];
return playerRows[ID];
}
return null;
}
And this is called when the game starts:
private PartnerMatchingPage m_PartnerPage;
private void StartMatching()
{
m_PartnerPage.gameObject.SetActive(true);
/* show row for self */
PartnerRowHandler row = m_PartnerPage.ActivateRow(PhotonNetwork.player);
}
Answer by Cyborgking · Jan 03, 2015 at 06:07 PM
It turned out that Start() is triggered after the rows have been taken (when they get taken they get enabled). This probably has something to do with that they are initially disabled. This immediatly untakes them and therefore disables them.
changing the function of Start() to this fixes the problem:
void Start()
{
if (!m_Taken)
Untake();
}