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 LakiLazuli · May 01, 2014 at 09:02 PM · rigidbodyyieldyield waitforsecondsoncollision

Problem with yield

Hello everybody,

I want to enable the rigidbody's gravity of an object by collision after waiting for 2 seconds. Here is my script so far (I am new to scripting):

 using UnityEngine;
 using System.Collections;
 
 public class Collision_Bridge : MonoBehaviour {
 
     void Start () {
     }
     
 
     void Update () {
     }
 
     void OnCollisionEnter(Collision other) {
         if (other.gameObject.tag == "Player") {
             StartCoroutine(waiting ());
         }
     }
 
     IEnumerator waiting()  {
         Debug.Log ("Before Waiting");
         yield return new WaitForSeconds(2);
         Debug.Log ("After Waiting");
         GetComponent<Rigidbody>().useGravity = true; 
     }
 
 }
 

My problem is that the rigidbody's gravity is enabled by the collision straight away and not after 2 seconds. Even if I put GetComponent().useGravity = true; in the Update funtion it does not work. "Before Waiting" and "After Waiting" is printed with an intervall of 2 seconds as I want it to be.

Does anybody know what is wrong? Thanks a lot!

Comment
Add comment · Show 8
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 Jinxology · May 01, 2014 at 09:11 PM 0
Share

So you are saying that the useGravity line is executing immediately, but the After Waiting line above it is not executing for 2 seconds?

avatar image LakiLazuli · May 01, 2014 at 09:16 PM 0
Share

Yes, that's right and I don't know why the useGravity line is executing immediately but the After Waiting line not.

avatar image theredace · May 01, 2014 at 09:18 PM 0
Share

Just to be sure, is the "Use Gravity" checkbox in the inspector unchecked by default?

avatar image Jinxology · May 01, 2014 at 09:24 PM 0
Share

Something to try, comment out that useGravity line and see if the gravity still kicks in.

avatar image LakiLazuli · May 01, 2014 at 09:45 PM 0
Share

The "Use Gravity" checkbox was unchecked but when I comment out that useGravity line the bridge keeps falling although there is no gravity. Could it depend on the rigidbody and the collision?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · May 01, 2014 at 10:04 PM

(EDIT: a new answer)

It strikes me that the useGravity line must not be the cause of this. I suspect gravity is being applied anyway for some reason, so the useGravity line is simply not having any effect at all.

I would try explicitly setting useGravity to false, and set the rigid body's velocity to 0 right at the start of that coroutine. We just need identify where the problem is occuring, so eliminating gravity and velocity at the start of your coroutine puts the rigid body in a known state.

It'll either be that the bridge already had gravity enabled, or the collision is causing it to fall - either by enabling gravity incorrectly or perhaps just knocking it downwards (giving the perception of gravity).

Once you've made those tweaks:

  • check useGravity is false

  • remove the useGravity=true line

  • set the wait to be 10s (just to give yourself some time)

  • add a Debug.Log to print useGravity at the start of your coroutine

  • add a Debug.Log to print useGravity inside your collision enter function

  • run the game

  • check useGravity is false

  • check the object isn't falling under gravity

  • hit pause immediately after collision

  • check useGravity is false

If that works, the bridge should just stay where it is. Adding back your useGravity=true line should bring it back. I suspect it'll then give you the desired effect, then we could try working out exactly what's causing the problem.

I assume there's nothing that fulls under gravity sitting on the bridge?

-Chris

(This next bit was wrong, and is what the comments below referred to!)

Subtle one. Your code should read startcoroutine("waiting") What you are doing is calling waiting just like a normal function. Then you are passing the return value of waiting into StartCoroutine!

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 theredace · May 01, 2014 at 10:07 PM 1
Share

Are you sure about that? I always call coroutines using the syntax StartCoroutine(myCoroutine()); and it definitely does not ignore the yield.

He already said above that even when commenting out the useGravity line the object still has gravity, so clearly the issue is elsewhere.

avatar image LakiLazuli · May 01, 2014 at 10:29 PM 0
Share

Yeah, I mean the Debug.Log lines work perfectly. I think it has something to do with the colliders. I keep thinking

(by the way I'm a girl ;) )

avatar image wibble82 · May 02, 2014 at 10:49 AM 0
Share

Ok - my bad. Learn a new thing every day!

avatar image LakiLazuli · May 03, 2014 at 01:37 PM 0
Share

So many thanks for all your help!

It had something to do with the rigidbody because the object as well as the player had one. As soon as I removed the rigidbody from the object it did not fall anymore. So I just attached the rigidbody after 2 seconds after collision. It works fine now :)

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i need to delay an action but my code isnt working 1 Answer

Physics not working properly 1 Answer

yield return new WaitForSeconds (); in c# 2 Answers

How do I fix yield not working in while loop? 1 Answer

yield WaitForSeconds(5); IS WAITING FOR EVER 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