- Home /
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;
}
}
}
}
}
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.
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.
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)
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.
Your answer
Follow this Question
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