WaitForSeconds in Coroutine not working as expected - c#

I want the bullet to start a Coroutine in the enemy script, which damages the enemy every second by a float given to the enemy script from the bullet, until it reaches 0 hp and destroys itself. For some reason the "Poisoning" process works until the waitforseconds place. Do I miss something?
When testing debug.log("STEP ONE") is always reached without any problems. But "Poisoned" after the WaitForSecond moment is never reached. I do not have any other errors. The Bullet script works perfectly fine for non Poisonous bullets.
public class Enemy : MonoBehaviour
{
...
public IEnumerator Poisoned(float HpPerSecond)
{
Debug.Log("STEP ONE");
yield return new WaitForSeconds(1);
Debug.Log("Poisoned");
TakeDamage(HpPerSecond);
StartCoroutine(Poisoned(HpPerSecond));
}
...
}
public class Bullet : MonoBehaviour
{
public bool Poisonous = false;
public float PoisonDamagePerSecond;
Coroutine poison = null;
...
void DamageTarget()
{
...
if (Poisonous)
{
poison = StartCoroutine(target.GetComponent<Enemy>().Poisoned(PoisonDamagePerSecond));
}
...
}
...
}

You need to understand about coroutines.
Check out the official documentation at the following link.
void DamageTarget()
{
...
if (Poisonous)
{
poison = StartCoroutine(target.GetComponent<Enemy>().Poisoned(PoisonDamagePerSecond));
}
...
}
Coroutines run in MonoBehaviour by default.
If you write the code as above, the coroutine is executed in Bullet.
When Bullet is disabled or destroyed the coroutine stops.
Try working in the form below.
Enemy
public void StartPoisoned(float HpPerSecond)
{
StartCoroutine(Poisoned(HpPerSecond));
}
Bullet
void DamageTarget()
{
...
if (Poisonous)
{
target.GetComponent<Enemy>().StartPoisoned(PoisonDamagePerSecond));
}
...
}

Related

Unity 2D: Making the Player Gameobject Reactivate after being turned off

I can't get the Player Gameobject to reappear. The player deactivates the moment the Timeline starts when they touch the box collider, but the player never reactivates. I have tried using Player.SetActive(true) in the coroutine and even using an Invoke method with no luck. Any ideas on how to fix this? Please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class TimelineTrigger : MonoBehaviour
{
// calling items for Unity
public PlayableDirector timeline;
public GameObject Player;
public GameObject CutsceneCollider;
public GameObject CutsceneMC;
// Start is called before the first frame update
void Start()
{
// calls the playable director and turns off the MC for the scene
timeline = timeline.GetComponent<PlayableDirector>();
CutsceneMC.SetActive(false);
}
void Update()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
private void EnableAfterTimeline()
{
Player = GameObject.FindGameObjectWithTag("Player");
Player.SetActive(true);
}
public void OnTriggerEnter2D (Collider2D col)
{
if (col.CompareTag("Player"))
{
// plays the cutscene and starts the timer
timeline.Play();
Player.SetActive(false);
Invoke("EnableAfterTimeline", 18);
CutsceneMC.SetActive(true);
StartCoroutine(FinishCut());
}
IEnumerator FinishCut()
{
// once the cutscene is over using the duration, turns off the collider and the MC.
yield return new WaitForSeconds(17);
CutsceneMC.SetActive(false);
CutsceneCollider.SetActive(false);
}
}
}
The issue here is that coroutines in Unity can't be run on inactive GameObjects, so FinishCut never gets executed.
This can be worked around by having a separate MonoBehaviour in the scene to which the responsibility of running a coroutine can be off-loaded. This even makes it possible to start static coroutines from static methods.
using System.Collections;
using UnityEngine;
[AddComponentMenu("")] // Hide in the Add Component menu to avoid cluttering it
public class CoroutineHandler : MonoBehaviour
{
private static MonoBehaviour monoBehaviour;
private static MonoBehaviour MonoBehaviour
{
get
{
var gameObject = new GameObject("CoroutineHandler");
gameObject.hideFlags = HideFlags.HideAndDontSave; // hide in the hierarchy
DontDestroyOnLoad(gameObject); // have the object persist from one scene to the next
monoBehaviour = gameObject.AddComponent<CoroutineHandler>();
return monoBehaviour;
}
}
public static new Coroutine StartCoroutine(IEnumerator coroutine)
{
return MonoBehaviour.StartCoroutine(coroutine);
}
}
Then you just need to tweak your code a little bit to use this CoroutineHandler to run the coroutine instead of your inactive GameObject.
public void OnTriggerEnter2D (Collider2D col)
{
if (col.CompareTag("Player"))
{
// plays the cutscene and starts the timer
timeline.Play();
Player.SetActive(false);
Invoke("EnableAfterTimeline", 18);
CutsceneMC.SetActive(true);
CoroutineHandler.StartCoroutine(FinishCut()); // <- Changed
}
IEnumerator FinishCut()
{
// once the cutscene is over using the duration, turns off the collider and the MC.
yield return new WaitForSeconds(17);
CutsceneMC.SetActive(false);
CutsceneCollider.SetActive(false);
}
}
If you have only one Player instance and it's accessible from the start, you'd better set it once in the Start method.
void Start()
{
// calls the playable director and turns off the MC for the scene
timeline = timeline.GetComponent<PlayableDirector>();
CutsceneMC.SetActive(false);
Player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
}
private void EnableAfterTimeline()
{
Player.SetActive(true);
}

How to call coroutine from another script? UNITY

This is the script where the touch action, the main game is on it
var addingGoldPerSec = new GameObject();
var buttonInstance = addingGoldPerSec.AddComponent<ItemButton>();
StartCoroutine(buttonInstance.addGoldLoop());
And this is the one where I have the coroutine
public double goldPerSec
{
get
{
if (!PlayerPrefs.HasKey("_goldPerSec"))
{
return 0;
}
string tmpStartGoldPerSec = PlayerPrefs.GetString("_goldPerSec");
return double.Parse(tmpStartGoldPerSec);
}
set
{
PlayerPrefs.SetString("_goldPerSec", value.ToString());
}
}
public void updateUI()
{
priceDisplayer.text = LargeNumber.ToString(currentCostPerSec).ToString();
coinDisplayer.text = "PER SEC\n" + LargeNumber.ToString(goldPerSec).ToString();
levelDisplayer.text = "LEVEL\n" + level + "/150".ToString();
}
public IEnumerator addGoldLoop()
{
while (DataController.Instance.gold <= 1e36)
{
DataController.Instance.gold += goldPerSec;
if (Gameplay.Instance.booster == 1)
{
yield return new WaitForSeconds(0.25f);
}
else if (Gameplay.Instance.booster == 0)
{
yield return new WaitForSeconds(1.0f);
}
}
}
And this is a third Script where I manage the data stored into PlayerPrefs
public void loadItemButton(ItemButton itemButton)
{
itemButton.level = PlayerPrefs.GetInt("_level",1);
PlayerPrefs.GetString("_costPerSec", itemButton.currentCostPerSec.ToString());
PlayerPrefs.GetString("_goldPerSec",itemButton.goldPerSec.ToString());
}
public void saveItemButton(ItemButton itemButton)
{
PlayerPrefs.SetInt("_level", itemButton.level);
PlayerPrefs.SetString("_costPerSec", itemButton.currentCostPerSec.ToString());
PlayerPrefs.SetString("_goldPerSec", itemButton.goldPerSec.ToString());
}
I have the second script which is the coroutine one attached into several gameobjects where exists an Upgrade button, per upgrade increases the coin by second you earn, the main problem here is that the coroutine just stops after I touch the screen, so I made another code into the main script so by that way the coroutine will keep working even after touched the screen, so I made a script where is the GameObject, but it just throws me NullReferenceException, tried a check with TryCatch and throws me that the problem is coming from the GameObject that I have created on the main script, but if is that way I need to attach to the main object where the main script exists, like more than 10 gameobjects where the coroutine exists, I think Singleton is not the way, it deletes me all the information above there by instantiating on Awake, I never thought about making static so I did as you told me and I need to change almost of my code, every text is attached into each gameobjects, to make a non-static member work with a static-member need to delete Monobehaviour but it just makes the game explode, thanks for helping me.
Create two scripts and attach them, for example, at Main Camera. The first script contains your timer with all variables:
using UnityEngine;
using System.Collections;
public class TimerToCall : MonoBehaviour {
//Create your variable
...
//Your function timer
public IEnumerator Counter() {
...
}
}
In the second script, you will call the timer:
public class callingTimer : MonoBehaviour {
void Start() {
//TimerToCall script linked to Main Camera, so
StartCoroutine(Camera.main.GetComponent<TimerToCall>().Counter());
}
}
if you want to have for example the second script not linked to any gameObjet you could use static property:
in first script linked:
void Start()
{
StartCoroutine(CallingTimer.ExampleCoroutine());
}
in second script not linked, you use the static property:
using System.Collections;
using UnityEngine;
public class CallingTimer
{
public static IEnumerator ExampleCoroutine()
{
//Print the time of when the function is first called.
Debug.Log("Started Coroutine at timestamp : " + Time.time);
//yield on a new YieldInstruction that waits for 5 seconds.
yield return new WaitForSeconds(5);
//After we have waited 5 seconds print the time again.
Debug.Log("Finished Coroutine at timestamp : " + Time.time);
}
}

Repeat function (unity3d/c#)

First of all, here's the code:
using UnityEngine;
using System.Collections;
namespace UltimateSurvival
{
public class Radiation : MonoBehaviour
{
public GameObject radiationEffect;
public EntityVitals Vitals { get { return m_Vitals; } }
private EntityVitals m_Vitals;
// Use this for initialization
void Start() {
InvokeRepeating ("OnTriggerEnter", 1.5f, 3.5f);
}
// Update is called once per frame
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
radiationEffect.SetActive(true);
//yield return new WaitForSeconds(5);
var entity = other.GetComponent<EntityEventHandler>();
if(entity)
{
var healthEventData = new HealthEventData(-Random.Range(7.0f, 23.0f));
entity.ChangeHealth.Try(healthEventData);
}
//yield return new WaitForSeconds(5);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
radiationEffect.SetActive(false);
}
}
}
}
What I'm trying to do is that I want this script to execute OnTriggerEnter every 3.5 seconds. As you can see, I'm using InvokeRepeating but it seems like it doesnt work. I've also tried changing void OnTriggerEnter on IENumerator OntriggerEnter and then yield return new WaitForSeconds(5); - It didn't work either. I'm really confused D: Please help!
It seems you're trying to solve the problem of draining HP from the player if player is inside the area of radiation. This is a solution that will use most of your current code, but is not neccesarily the best code. I'd also like to inform you of OnTriggerStay, which
is called once per frame for every Collider other that is touching the trigger.
and can also be used to solve this problem. I'm going to use your already declared OnTriggerEnter and OnTriggerExit to damage every player inside area every 3.5 seconds.
public GameObject radiationEffect;
public EntityVitals Vitals { get { return m_Vitals; } }
private EntityVitals m_Vitals;
// Declare a list that will contain the players.
List<GameObject> playersInArea = new List<GameObject>();
// Use this for initialization
void Start() {
InvokeRepeating ("DamagePlayers", 1.5f, 3.5f);
}
void DamagePlayers() {
foreach (var player in playersInArea) {
var entity = player.GetComponent<EntityEventHandler>();
if(entity)
{
var healthEventData = new HealthEventData(-Random.Range(7.0f, 23.0f));
entity.ChangeHealth.Try(healthEventData);
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
playersInArea.Add(other.gameObject);
radiationEffect.SetActive(true);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
playersInArea.Remove(other.gameObject);
if (playersInArea.Count == 0) {
radiationEffect.SetActive(false);
}
}
}
Something like that should work. If you only have 1 player it should work all the same, but this supports multiple players. Let me know if you have any further issues.
You have problems calling your method using InvokeRepeating because of two reasons:
InvokeRepeating can not be used with method that have parameters: you probably have the Trying to Invoke method: [...] couldn't be called. message in your console.
OnTriggerEnter is a method that is automatically called by Unity when the gameobject's collider is set as Trigger and another collider enters it: as #Hellium said, calling such methods manually is a bad idea (same as calling Start or Update manually: this can happen but really doesn't make sense in most of the scenarios).
Hope this helps,

How do I pause GameObject for a couple of seconds and unpause after a couple of seconds in unity3d

I need the gameobject to pause on its own in my scene for 7f or 8f and un-pause at 2f on its own. The script I have is letting my pause by key. Here is my script :
{
sing UnityEngine;
using System.Collections;
public class star : MonoBehaviour {
GameObject[] pauseObjects;
void Start () {
pauseObjects = GameObject.FindGameObjectsWithTag("Player");
}
void pauseGameobject()
{
if()
{
start coroutine("wait");
}
}
public ienumenator wait()
{
time.timescale = 0;
yield return new waitforsceonds(7);
time.timesale = 1;
}
void pauseGameobject()
{
if()
{
start coroutine("wait");
}
}
public ienumenator wait()
{
time.timescale = 0;
yield return new waitforsceonds(7);
time.timesale = 1;
}
}
You can use coroutines to insert delays in the update() loop. Coroutines use generators, which "yield", rather than functions/methods which "return". What this allows for is code that operates asynchronously while still being written in a linear fashion.
The built in coroutine you're most likely looking for is WaitForSeconds. To start a coroutine you simply call StarCoroutine() and pass in any method of type IEnumerator. This method will yield periodically. In the following example, WaitForSeconds(5) will yield after 5 seconds. Fractions of a second can also be used, represented by floats, for example 2.5 would be two and a half seconds.
using UnityEngine;
using System.Collections;
public class WaitForSecondsExample : MonoBehaviour {
void Start() {
StartCoroutine(Example());
}
IEnumerator Example() {
Debug.Log(Time.time); // time before wait
yield return new WaitForSeconds(5);
Debug.Log(Time.time); // time after wait
}
}
It's not too clear what you mean by pause on it's one, however I'm going to answer broadly, to try and help you.
If you whant to pause a single game object externally you can deactivate it and activate it accordingly with this code: gameObject.SetActive(false);
Instead if you want to pause the game object internally you can make a bool and in the update test wether or not it's true:
using UnityEngine;
using System.Collections;
bool update = false
public class ActiveObjects : MonoBehaviour
{
void Start ()
{
//Do stuff
}
void Update ()
{
if(update){
//Do stuff
}
//decide wether or not to pause the game object
}
}
If you want to pause the game you can set the Time.timeScale to 0, or just pause all game objects.
Here you can find how to make a timer, all you need to do is count down a variable using timeLeft -= Time.deltaTime;.
Hope I helped you,
Alex
Edit:
Ok, here is the script, keep in mind I have no way to test it ;)
using UnityEngine;
using System.Collections;
public class star : MonoBehaviour {
GameObject[] pauseObjects;
public float timer = 7;
float t = 0;
bool pause = false;
void Start () {
pauseObjects = GameObject.FindGameObjectsWithTag("Player");
t = timer;
}
void Update() {
if(pause){
if(t<0){
t=timer;
pause = false;
time.timescale = 1;
}else{
t -= Time.deltaTime;
time.timescale = 0;
}
}
}

Coroutine stops working

I have two scripts with a coroutine in them. It works perfectly fine in the first one, but not in the second one, for no apparent reason.
It works in this one:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.ImageEffects;
public class GameStartController : MonoBehaviour {
public Button startButton;
public GameObject cubeSpawner;
// Use this for initialization
private void Start() {
startButton = startButton.GetComponent<Button>();
}
public void StartGame() {
EnableCubeSpawner();
SpawnStartingCubes();
HideStartMenu();
StartCoroutine("FocusCamera");
PlayBackgroundMusic();
}
// Enables the cube spawner, so it can start spawning cubes
private void EnableCubeSpawner() {
cubeSpawner.SetActive(true);
}
private void SpawnStartingCubes() {
cubeSpawner.GetComponent<CubeSpawner>().GenerateStartingCubes();
}
private void PlayBackgroundMusic() {
var audio = GameObject.FindWithTag("Audio").GetComponent<AudioController>();
audio.PlayBackgroundMusic();
}
private void HideStartMenu() {
startButton.transform.parent.GetComponent<CanvasGroup>().interactable = false;
startButton.transform.parent.GetComponent<CanvasGroup>().alpha = 0f;
}
private IEnumerator FocusCamera() {
var camera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
var velocity = 0f;
while (Mathf.Abs(camera.GetComponent<DepthOfField>().aperture) > 0.001f) {
Debug.Log(Mathf.Abs(camera.GetComponent<DepthOfField>().aperture));
camera.GetComponent<DepthOfField>().aperture = Mathf.SmoothDamp(camera.GetComponent<DepthOfField>().aperture, 0f, ref velocity, 0.3f);
yield return null;
}
camera.GetComponent<DepthOfField>().aperture = 0f;
}
}
The coroutine works just fine and the camera aperture goes smoothly from 0.6 to 0.
However in the second script this doesn't happen:
using System.Collections;
using System.Linq;
using UnityEngine;
using UnityStandardAssets.ImageEffects;
public class GameOverController : MonoBehaviour {
public void EndGame() {
StartCoroutine("UnfocusCamera");
DisableCubeSpawner();
DestroyAllCubes();
StopBackgroundMusic();
ShowStartMenu();
}
// Disables the cube spawner, so it can stop spawning cubes
private void DisableCubeSpawner() {
var cubeSpawner = GameObject.FindWithTag("CubeSpawner");
cubeSpawner.SetActive(false);
}
private void DestroyAllCubes() {
var gameObjects = FindObjectsOfType(typeof(GameObject));
foreach (var gameObject in gameObjects.Where(gameObject => gameObject.name.Contains("Cube"))) {
Destroy(gameObject);
}
}
private void StopBackgroundMusic() {
var audio = GameObject.FindWithTag("Audio").GetComponent<AudioController>();
audio.StopBackgroundMusic();
}
private void ShowStartMenu() {
var startMenu = GameObject.FindWithTag("StartMenu");
startMenu.GetComponent<CanvasGroup>().interactable = true;
startMenu.GetComponent<CanvasGroup>().alpha = 1f;
}
private IEnumerator UnfocusCamera() {
var camera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
var velocity = 0f;
while (camera.GetComponent<DepthOfField>().aperture < 0.6f) {
Debug.Log(Mathf.Abs(camera.GetComponent<DepthOfField>().aperture));
camera.GetComponent<DepthOfField>().aperture = Mathf.SmoothDamp(camera.GetComponent<DepthOfField>().aperture, 0.6f, ref velocity, 0.3f);
yield return null;
}
// camera.GetComponent<DepthOfField>().aperture = 0f;
}
}
It only works for one frame (aperture goes from 0 to 0.03), and then just stops.
Why is this happening?
If you destroy (or disable) a game object, coroutines running on components attached to it will stop. I'm unable to find a primary source on this, but here are two other stack overflow questions where this was the problem:
Unity3d coroutine stops after while-loop
Unity - WaitForSeconds() does not work
The coroutine stops because the GameObject your GameOverController is attached to is destroyed. Presumably Unity checks whether an object still exists before resuming its coroutine, and if the object is destroyed, Unity does not continue to execute it.
To fix this problem, you can delay destroying the GameObject until the animation is complete (perhaps putting the destroy code after the while loop in the coroutine) or put the component on a GameObject that won't be destroyed.
In most cases this mean that your object or script became inactive. Best way to check this is to add OnDisable() method to your script and call logging from it

Categories