- Home /
Check if child exists and instantiate as child
I have a Gas Can prefab that consists of a parent and a single child. The parent is an empty game object. The child is the visible animated gas tank, with a collider, and the script that raises the players fuel and deletes its self upon colliding with the player. I want another script that instantiates a new child after say 30 seconds have passed, right where the old child was, if the old child has already been destroyed. If there's a better way to do this, I'd appreciate it. It's kind of a primary aspect of my game.
Answer by robertbu · Jul 15, 2014 at 10:49 PM
This question is difficult to answer accurately without know more about your situation. In particular:
Is the child you want to spawn after 30 seconds also the gas can?
You say "if the child has been destroyed" which implies that the 30 second timer is triggered by something other than the destruction of the gas tank.
You say right where the old child was. Is that the same local position or the same world position (only makes a difference if the empty parent object moves)?
Let me assume that some event happens to 'destroy' the gas can. Might be hitting it, might be going empty, might be some trigger. And that you want the gas can to reappear at the same local position after 30 seconds. If so a good approach would be to hide the gas can rather than destroy it. The outline of the logic for the script might look like this:
function Hide() {
animation.Stop();
collider.enabled = false;
renderer.enabled = false;
Invoke("Show", 30.0);
}
function Show() {
collider.enabled = true;
animation.Play();
renerer.enabled = true;
}
And the script would obviously go on the gas can. You would call Hide() when whatever event occurs that 'destorys' the gas can.
When I read this I thought for sure it would work. It makes perfect sense to me. However, the first time I tried it, it returned an error saying that there was no animation on the object is was trying to access. I check and made sure the script was on the the right object, and it was. I don't know much about animation, but I do know that the object is animated because I am literally watching it move around as I had defined when I made the animation. It does have an "animator" component on it. So I commented the animation lines out temporarily. Then, before playing the game, the error was that there were no renderers attached to the game object. So I commented out the renderer lines. As intended, the collider lines, and the call to Hide() worked perfect, and I could only use the gas tank once every 30 seconds. Here is my script.
function OnCollisionEnter (item : Collision){
if(item.gameObject.name == "$$anonymous$$onster Truck"){ //$$anonymous$$onster Truck is the player
Stats.fuel += 15; //add 15 to fuel var in Stats script.
Hide();
}
}
function Hide() {
// animator.Stop();
collider.enabled = false;
// renderer.enabled = false;
Invoke("Show", 30.0);
}
function Show() {
collider.enabled = true;
// animation.Play();
// renderer.enabled = true;
}
Everything you assumed before answering my question was true. The event that happens to trigger the hide function is the player colliding with it, and I do want it to reappear at the same local position. Here is a picture of the inspector of the object I want to hide
Animator is $$anonymous$$ecanim. Frankly I've never done anything with $$anonymous$$ecanim (has not been necessary with my 'real' Unity work). I probably should spend a bit of time there. As a guess based on a quick read of the reference, you can replace animator.Stop() and animation.Play() with:
GetComponent(Animator).StopPlayback();
GetComponent(Animator).Play();
As for the renderer, is the visible object a child of this object? If so, you want to enable/disable that renderer.
So the problem with the renderer is that the gas can model was actually two parts that were children of the object I was trying to hide. So the hierarchy looked like this
Gas Can Parent > Gas Can Child > Part 1 Part 2
So I tried this line:
GetComponentInChildren(Renderer).enabled = false;
And it worked fine, except that it only hid the part 1 child and not part 2, so the nozzle of the gas can remained floating in the air without the tank. Which I fixed buy editing the model in $$anonymous$$aya to be one piece. So thank you so much for helping me out, i learned a lot in the process.
If you have multiple children, you'll have to do something like this:
var rends = gameObject.GetComponentsInChildren(Renderer);
for (var rend : Renderer in rends)
rend.enabled = false;
Your answer
Follow this Question
Related Questions
Instantiate prefabs just before it comes into view 2 Answers
Instantiate prefabs before it comes into view 0 Answers
how to destroy camera instatiated from prefab? 0 Answers
Combining Meshes? 1 Answer