- Home /
Display one of prefab child
First of I am very new to Unity.
I have a script that places either prefab1 or prefab2 depending what keyboard button have been clicked.
Super simple:
prefab1 prefab2
--child1 --child1
--child2 --child2
--child3 --child3
First of when I place the prefab it (of course) shows all models, that's not what I want. I want to place/show only the first child.
Then I want to "age" the prefab. After some time it should change to prefab1.child2 then after some more time it should change to prefab1.child3 and so on.
I don't want to hardcode children and such, that is where I encounter problems.
My question is:
Can I place a prefabs child then change that later? something like this pseudocode:
if buttonClicked = "1"
activePrefab = prefab1
if buttonClicked = "2"
activePrefab = prefab2
buttonClicked = "space"
child = activePrefab.transform.GetChild(0).gameObject
Instantiate (child , player.position, player.rotation)
age = age + (Time.time)
if age > 5
newChild = activePrefab.transform.GetChild(1).gameObject
Instantiate (newChild, child.position, child.rotation)
Does anyone have any ideas, know of an old thread, or seen a video somewhere about it?
Regards, Jerakin
Answer by ZenithCode · May 01, 2014 at 03:52 PM
I would suggest you write an 'aging' script and put it on the parent object.
On Awake() it hides all models, then as time goes by, the model is swapped according to your needs.
That sounds like a great idea, I have tried that approach already but couldn't really get them to be hidden. I tried to do it with GameObject.Find("child1").renderer.enabled = false; But even though I saw that it did set it to false in the inspector it didn't hide it. Do you got an idea why that was the case?
I got it to work with gameObject.transform.GetChild(0).gameObject.SetActive(false);
Your answer
Follow this Question
Related Questions
Changing prefab doesn't update instances (in scene aswell as in other prefabs).. 3 Answers
Prefab not changing variable's values 2 Answers
Why is instantiated animator prefabs are not working properly? 2 Answers
What process should I best use to properly create an updated copy of a Prefab? 1 Answer
Replacing a prefab with a new one to update model changes 1 Answer