Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hydropulse17 · Jul 15, 2014 at 10:36 PM · javascriptinstantiateprefabdestroychild

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hydropulse17 · Jul 15, 2014 at 11:44 PM 0
Share

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 hidealt text

capture.jpg (48.6 kB)
avatar image robertbu · Jul 16, 2014 at 01:18 AM 0
Share

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.

avatar image Hydropulse17 · Jul 16, 2014 at 07:24 PM 0
Share

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.

gas can.jpg (228.1 kB)
avatar image robertbu · Jul 17, 2014 at 12:31 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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

Instantiated gameObject(here, wall) destroyed, but when checked for existence, shows that it exists, how? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges