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 BritishGuy · Oct 28, 2014 at 11:23 AM · c#yielddelayinvoke

Delay If statement in Update c#

Hello,

I seem to be having some problems after converting a JS to C#, I used a Invoke to get the results i wanted but dont seem to work in c# for some reason, What i want to achieve is for the GetButton to be delayed by what ever value i set publicly, Any ideas suggestions? Thanks.

 using UnityEngine;
 using System.Collections;
 
 public class RayCastTree : MonoBehaviour {
 
 
 public int rayLength = 10;
 private PlayerControl playerAnim;
 public GameObject tree;
 
     void  Update (){
         RaycastHit hit;
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
     
         if(Physics.Raycast(transform.position, fwd, out hit, rayLength))
         {
             if(hit.collider.gameObject.tag == "Tree")
             {
                 tree = (hit.collider.gameObject);
                 playerAnim = GameObject.Find("Katana_Anim").GetComponent<PlayerControl>();
             
                 if(Input.GetButton("Fire"))
                 {
                     tree.GetComponent<TreeController>().treeHealth -= 1;
                 }
             }
         }
     }
 }
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 gjf · Oct 28, 2014 at 11:58 AM

maybe something like...

 public float FireDelay = 2.0f;
 private float fireTime;

 private PlayerControl _playerControl;

 void Awake()
 {
     //  cached here, but it's not even being used...
     _playerControl = GameObject.Find("Katana_Anim").GetComponent<PlayerControl>();\

     fireTime = Time.time + FireDelay;
 }

 void Update ()
 {
     RaycastHit hit;
     Vector3 fwd = transform.TransformDirection(Vector3.forward);

     bool canFire = false;

     if(Physics.Raycast(transform.position, fwd, out hit, rayLength))
     {
         if(hit.collider.gameObject.tag == "Tree")
         {
             tree = hit.collider.gameObject;
             canFire = true;
         }
     }

     if ((fireTime > Time.time) && canFire)
     {
         fireTime = Time.time + FireDelay;

         if(Input.GetButton("Fire"))
         {
             if (tree != null)
             {
                 TreeController treeController = tree.GetComponent<TreeController>();

                 if (treeController != null)
                 {
                     treeController.treeHealth -= 1;
                 }
             }
         }
     }
 }

EDIT: this is untested = i'm not near my dev box.

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 BritishGuy · Oct 28, 2014 at 12:59 PM 0
Share

Thank you for replying, I am unable to get your script working, I don't get any errors or anything, It just wont take any damage from the tree, I have tried editing my original script to make it a coroutine but that does not seem to work either, I have updated my main post with the full original script, Hope it helps thanks again.

avatar image gjf · Oct 28, 2014 at 01:14 PM 0
Share

your problem is likely to be related to the fact that you not only need to be hitting the tree (throw in some Debug.Log's to see whether it's hitting) but also that you need to be pressing fire at the same time.

the code that i wrote (as i added, untested - sorry, not near my dev box) will check for fire every so many seconds - in this case, 2 seconds. add a Debug.Log into the timer code to see how often that's triggering too!

in my experience (27yrs since my first game was published), adding the debug info when things aren't working often leads to those light bulb moments or at the very least, helps to narrow down where the actual problem is. after re-reading, the script doesn't look so wrong - perhaps somebody else could comment...

EDIT:

maybe this

 if ((fireTime > Time.time) && canFire)

should be

 if ((fireTime < Time.time) && canFire)

avatar image
1

Answer by Bieere · Oct 28, 2014 at 02:07 PM

You could always use a coroutine to implement a delay without having to check the time passed every single frame within the update:

 IEnumerator waitTime()
     {
         while (true)
         {
             yield return new WaitForSeconds(Time);
             FireButton.SetActive(true);
         }
     }

and then set its active to false when it's been pressed in the update function.

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

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

27 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to translate code with yield waitforseconds to c#? 1 Answer

Delay in a IEnumerator 2 Answers

Create a delay between function executions 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