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
1
Question by MatthewSmoothy · Aug 06, 2012 at 10:59 AM · c#ontriggerenteryieldwaitforsecondscoroutines

Yield/OnTriggerEnter Help C#

Hey guys, at the moment my rocks are falling due to having a rigidbody and being placed in the sky with gravity bringing them down. When they hit the player it sends the player to the beginning of the level using Application.LoadLevel(2). I am fairly new and have attempted yield wait for seconds and coroutines but have not got the code correct. I would like it so when the rock hits the player their is a 2 second delay then the player restarts the level. Also I want these rocks to be triggers so when the player hits the box collider the rock will begin to fall. Thank you for any help, greatly appreciated. :)

 using UnityEngine;
 using System.Collections;
 
 public class fallingrock: MonoBehaviour {
  
  
     public float Minspeed;
  
     public float Maxspeed;
  
     private float currentSpeed;
  
     public GameObject explosion;
  
     // Use this for initialization
     void Start()     
     {
         currentSpeed = Random.Range(Minspeed, Maxspeed);
  
         float amtToMove = currentSpeed * Time.deltaTime;
         transform.Translate(Vector3.down * amtToMove) ;
  
     }
  
     // Update is called once per frame
     void Update ()     
     {
         transform.Rotate(Vector3.forward * Time.deltaTime * 100);
     }    
  
     void OnCollisionEnter(Collision mColl)
     {     
         //Detects when asteroid hits terrain     
         if(gameObject.tag == "Asteroid" && mColl.gameObject.tag == "Terrain")
         {         
             Destroy (gameObject);
             GameObject explosCLONE;
             explosCLONE = Instantiate(explosion, transform.position, transform.rotation) as GameObject;
         }
  
         //detects when asteroid hits player
  
         if(gameObject.tag == "Asteroid" && mColl.gameObject.tag == "Player")
         {     
             Destroy (gameObject);
             GameObject explosCLONE;
             explosCLONE = Instantiate(explosion,transform.position,transform.rotation) as GameObject;
             Application.LoadLevel(2);     
         }     
     }
 }
Comment
Add comment · Show 3
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 Kryptos · Aug 06, 2012 at 11:38 AM 1
Share

Formatted your code. Please do it properly next time by using the "101010" button.

avatar image MatthewSmoothy · Aug 06, 2012 at 11:47 AM 0
Share

Thank you sorry

avatar image Kryptos · Aug 06, 2012 at 12:00 PM 0
Share

No problem. When you get enough karma (15 I think), you will be able to edit your own post.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Aug 06, 2012 at 11:02 AM

On the guy:

 function OnCollisionEnter(other:Collision){
   if(other.gameObject.tag=="Stone") Restart();}
 
 function Restart(){
   yield WaitForSeconds(2);
   transform.position= new Vector3(0,0,0);
 }

I guess that would work but I don't have Unity to try.

Comment
Add comment · Show 2 · 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 MatthewSmoothy · Aug 06, 2012 at 11:47 AM 0
Share

Im using C#, would it just be a matter of changing function to void?

avatar image Kryptos · Aug 06, 2012 at 12:07 PM 0
Share

In C#, coroutine must return an IEnumerator object.

OnCollisionEnter can already be a coroutine (if I'm not mistaken). So changing @fafase code into C#, it gives:

 IEnumeratorOnCollisionEnter(Collision other)
 {
     if(other.gameObject.tag=="Stone")
     {
         yield return new WaitForSeconds(2);
         transform.position = new Vector3(0,0,0); // to reset the position
         // or Application.LoadLevel(Application.loadedLevel); // to restart the level
     }
 }

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

9 People are following this question.

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

Related Questions

Mysteries of yield 1 Answer

Trouble Resuming after Yielding while Inside Coroutine 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# yield waitforseconds 3 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