Can't quite figure out how to make this bit of code work. Help please?
I'm still somewhat of a beginner, but I'm trying to figure out how to make this work and I cannot seem to find anything quite like what I'm looking for. The idea of this script is that when the HP bar appears when the player is hit. That works as the hitState is activated. The problem is it never deactivates. I've tried adding a timer via WaitForSeconds and trying to have it switch back off. However, it tells me that on line 44 that the best overloaded math for StartCoroutine has some invalid arguments. The second error is also on line 44 as it says argument #1 cannot covert 'method' group' expression to type 'System.Collections.IEnumerator'
I am aware the code looks a bit weird as it's also finding another component from another script. Pretty much the clampTag only serves to pin the HP bar UI above the player's head at all times. The below is the script in question.
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HpBarBehavior : MonoBehaviour
{
public Image UiTag;
public bool hitState = false;
public float fadeTime;
public float fadeInstruction;
private const float BarFadeDuration = 1.0f;//time tofade in/out the bar
private float _fadeTimer = 0.0f;
private float timeSinceLastCombat = 10000.0f;
private const float CombatTimeout = 1.0f;//how long till no incombats starts a fadeout. so that things dont bounce on/off too much
private float barTimer = 0.0f;
private float barAlpha = 0.0f;
void Start()
{
if (hitState == false)
{
UiTag.gameObject.SetActive(false);
GameObject.Find("ThirdPersonController").GetComponent<PlayerHealth>();
GameObject.Find("tagHolder").GetComponent<ClampTag>();
}
}
public void inCombat()// call at start of, while still in combat
{
timeSinceLastCombat = 0.0f;
}
// Update is called once per frame
public void Update()
{
if (hitState == true)
{
UiTag.gameObject.SetActive(true);
GameObject.Find("ThirdPersonController").GetComponent<PlayerHealth>();
GameObject.Find("tagHolder").GetComponent<ClampTag>();
StartCoroutine(timerCount);
}
}
public void OnCollisionEnter(Collision collision)
{
Debug.Log(collision.gameObject.tag);
if (collision.gameObject.tag == "Hit")
{
hitState = true;
}
}
IEnumerator timerCount()
{
yield return new WaitForSeconds(5);
UiTag.gameObject.SetActive(false);
GameObject.Find("ThirdPersonController").GetComponent<PlayerHealth>();
GameObject.Find("tagHolder").GetComponent<ClampTag>();
}
}
Well timerCount is a function, not a variable, you need to call it. If you look at the examples here: https://unity3d.com/learn/tutorials/topics/scripting/coroutines you'll see that it's being called. Not sure if that's exactly your problem but it's worth testing out.
I will most definitely try that out. I did try Coroutines before but I didn't properly understand them.
Answer by BastianUrbach · Jan 02, 2018 at 11:25 PM
Try
StartCoroutine(timerCount());instead of
StartCoroutine(timerCount);
Your answer
![](https://koobas.hobune.stream/wayback/20220612144757im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I need help getting a camera to fade up from black after my player teleports to next room position. 2 Answers
[Solved] Fading Two Materials Using Color 0 Answers
Screen Fader Problem 1 Answer
How to add a scene fader to a timer 0 Answers
How to fade in and out a image in a loop?,How do I fade in and out image on a loop? 0 Answers