• 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 himanshu619 · Dec 24, 2012 at 02:25 PM · gameobjectdisappear

Game object disappears when player is onto it

Hi, I want that a particular gameobject disappears if my player is onto it for some x duration. I'm using following script, But it disappear gameobject instantly:

function OnCollisionEnter(other : Collider ) { if (tag == "Pickup") yield WaitForSeconds(5); Destroy(gameObject); }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MaGuSware™ · Dec 24, 2012 at 02:31 PM

There is an answer in someone elses question: http://answers.unity3d.com/questions/138725/cancelling-destroygameobject-time.html

instead of function Start() use function OnCollisionEnter() and instead of BlahWhatever(), use OnCollisionExit()

you also may want to check to make sure that the other.tag == "Player".

-edit-

 private var collidedObject : GameObject = null;

 function DoTheThing()
 {
     if(collidedObject != null)
         collidedObject.SendMessage("FunctionToKnockOver");

     Destroy(gameObject);
 }

 function OnCollisionEnter( collision : Collision )
 {
     if(collision.gameObject.tag == "Player")
     {
         collidedObject = collision.GameObject;
         Invoke("DoTheThing", 2);
     }
 } 

 function OnCollisionExit( collision : Collision )
 {
     if (collision.gameObject == collidedObject)
     {
         collidedObject = null;
     }
 }

As for making the player fall. You will have to write some code for that yourself, because I don't know how your player is currently setup. To make the falling work, the player will need a script on it that contains a function called "FunctionToKnockOver".

Comment
Add comment · Show 29 · 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 himanshu619 · Dec 25, 2012 at 08:08 AM 0
Share

Hi, $$anonymous$$aGuSware thanks for you kind reply. But it's still not working for me. What, I want is when my player stand on a gameobject, then after particular time (e.g. 2 sec.) it hide automatically and player falls if it's still on it! It would be great if you can edit my script and post it your answer.

avatar image MaGuSware™ · Dec 25, 2012 at 08:46 AM 0
Share

Well the solution posted should still work for that. Ill post a script which should work.

avatar image himanshu619 · Dec 25, 2012 at 08:48 AM 0
Share

Thanks in advance...

avatar image MaGuSware™ · Dec 25, 2012 at 08:58 AM 0
Share

Update posted, note: I have not tested that, but it should work.

avatar image MaGuSware™ · Dec 27, 2012 at 02:58 PM 1
Share

Firt of all, get rid of the "Destroy(gameObject);", kinda of an obvious first step lol.

Then it will need a rigid body. If it does not have one you can attach one with script.

gameObject.AddComponent(Rigidbody);

Or if it already has a rigid body, just set the useGravity.

gameObject.rigidbody.useGravity = true;

Show more comments
avatar image
0

Answer by Imankit · Dec 25, 2012 at 05:16 PM

You can also do something like this.. using a Coroutine.. Its more flexible...

IENumerator DestroyObject(){

yield return new WaitForSeconds (5f);

Destroy(gameObject);

}

void OnTriggerEnter(collider other){

if (other.gameobject.tag == "Pickup"){

StartCoroutine(DestroyObject());

}

}

Comment
Add comment · Show 3 · 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 fafase · Dec 25, 2012 at 05:59 PM 0
Share

why using a coroutine when Destroy already has a timer?

 Destroy(ref,5.0f);
avatar image himanshu619 · Dec 26, 2012 at 03:57 AM 0
Share

Thanks to all for your kind support!

avatar image Imankit · Dec 26, 2012 at 04:37 AM 0
Share

You are right that destroy already has a timer but for flexibility of using a function anywhere when needed is provided by coroutine.. Also it is recommended to use coroutine in areas returning time...

Also garbage collection of coroutine is much faster..

In future if you have FPS issues than u can use Invoke or Coroutine which will surely alter any FPS issue..

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't see my gameobjects in scene view 1 Answer

When I build the project gameObjects disappear 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Game Object disappears from inspector when I press play 1 Answer

How do you make a GameObject appear on a trigger? 2 Answers


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