- Home /
Can't activate my deactivated GameObjects
I'm creating GameObjects based on prefabs and add them to a list. Later i want to be able to deactivate and activate those GameObjects. But all i can do is deactivate them. After that I'm not able to activate them again. Thats how i do it:
as a member variable:
private List<GameObject> activatedModels = new List<GameObject>();
then in my method:
GameObject newPOI = new GameObject();
newPOI = Instantiate(this.myPrefab) as GameObject;
this.activatedModels.Add(newPOI);
then in a public method to be accessable for another scripts:
public void changeModelState(bool state) {
foreach(GameObject model in this.activatedModels) {
model.SetActive(state);
}
}
Deactivating my models with this method is working, but activating them again is not. I thought maybe i lose their reference after deactivating them, but the list still contains all models after doing so and I can even access their methods and so on.
Maybe someone got a clue whats wrong here? Thanks in advance! :)
Are you sure :
There is no deactivated parent
change$$anonymous$$odelState
is called twice, the first time withtrue
, and immediately after with withfalse
? (Put a simpleDebug.Log
to check)
Unfortunately I am. Only thing I'm deactivating in my scripts are those models and these are not bound to any other GameObjects which could be deactivated. And i checked - this method is properly being called with true and false!
Answer by Kounex · Oct 06, 2017 at 10:57 AM
I found the problem. What i didn't mention is that i made some animation for my models. I'm using the Animator
to represent 3 different states: model_default
, model_start
and model_end
. My model_default is the entry point for this animation and has the value active
on false
.
After initially creating this model from its prefab i call:
newPOI.GetComponent<Animator>().SetTrigger("model_start");
where it gets scaled and active
is set to true
. Then I'm deactivating and activating my model again. After that it seems to reset its animation state back to model_default
where active
is on false
!
Solution: I need to change the animation state again! Sorry for not knowing this! :x