Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
-3
Question by Zitoox · Nov 08, 2016 at 07:24 PM · c#scripting problemwaitforseconds

wait for seconds in c#

I know that i need to use IEnumerator MyMethod() and make a coroutine in order to make a script wait for seconds and then do something, but i can't do this inside my script =\

So, i have a OnCollision void and i wanted to use wait for seconds on it, like:

 void OnCollisionEnter(Collision collision)
 {
     Wait 2 seconds
     Do everything that i want
 }

But i can't. I am having multiple problems, such as "this thing shouldn't be here", "you can't do this" and all. I have read the docs about this a lot of times but this just doesn't work.

Here is my script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 public class EAimSys : MonoBehaviour //This is my Enemy script. As it doesn't have any mouse or keyboard, i need to use the WFS code.
 {
     bool Click = false;
     bool target = false;
 
     public float fireRate = 0.5F;
     private float nextFire = 0.0F;
     public GameObject Flash;
     public Animator anim;
 
 
 
 
     void OnCollisionEnter(Collision collision)
     {
         {
             if (collision.gameObject.tag == "Player") //Here is where i want to start.
 
                 //I wanted to use the wait for seconds code in here, so everything below this would need to wait x seconds before happening.
                 //Why? Because otherwise my enemy would kill me instantly and i don't have enough experience to do anything more complicated than this.
                 anim.SetTrigger("Shoot");
             AudioSource audio = GetComponent<AudioSource>();
             audio.Play();
             Flash.SetActive(true);
             Debug.Log("Player Hit!");
         }
     }
 
     void OnCollisionExit(Collision collision)
     {
         {
             if (collision.gameObject.tag == "Enemy")
 
             Flash.SetActive(false);
         }
     }
 }
 






Comment
Add comment · Show 2
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 Zitoox · Nov 08, 2016 at 07:25 PM -1
Share

Also sorry, but i couldn't find an answer for this so i needed to ask. After fixing this problem i will be almost done with the scripting part and i can finally release my game as the nice fps it is =')

avatar image TBruce Zitoox · Nov 10, 2016 at 06:37 PM 0
Share

Did you try my solution below. Besides the links to script, I provided a link to a demo Unity package. Please take a look at it.

It is a rough demo with a square (enemy) shooting (instantiating) a projectile at a sphere (player) every 2 seconds.

I did not add movement controls to the player but the sphere can be moved at run time in the scene view.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SrXoo · Nov 08, 2016 at 09:04 PM

Well, my answer might be considered a little "dirty" but it is a possible solution.

You could delegate all that logic to other method which updates in the Update(). Having a float variable with the seconds you want to wait and reduce it using deltaTime (1 second = 1f).

Comment
Add comment · 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
0

Answer by Razputin · Nov 08, 2016 at 09:04 PM

use coroutines

     void OnCollisionEnter(Collision collision)
  {
 StartCoroutine(Fire());
  }
 
 Private Ienumerator Fire()
 {
 // do whatever you have to do to fire
 yield return new WaitForSeconds(2);
 StartCoroutine(Fire()); //if you put this here it will start the coroutine again over and over after 2 sec
 }

Comment
Add comment · Show 1 · 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 Zitoox · Nov 08, 2016 at 10:02 PM -1
Share

Stil not working...

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

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

Related Questions

StartCoroutine not listening to parameters 1 Answer

I want my script to wait 2 seconds before continue in a condition, in update, using C# 2 Answers

Hololens sharing example 1 Answer

How should I use the `FreeLookCam` script located in the standard assets folder 0 Answers

[C#]How to save a list of items to use in another script and it's not a player prefs type of list? 0 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