[Unity] WaitForSecondsが正しく実行されない
解決したいこと
UnityでPlayerのパラメータを更新し、一定時間後に戻す処理を作りたいのですが、コルーチン内のWaitForSecondsが正しく実行されません。 (下記のコードで Debug.Log("A"); は実行されるが、B以降はロギングされない。)
また、コメントアウトしている通り、Invokeでも試しましたが、これも動きません。(DもEもロギングされない。)
解決方法を教えて下さい。
該当するソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemGreen : MonoBehaviour
{
private GameObject Player;
public float effectseconds = 1.0f;
public float JumpUpRate = 2.0f;
private float baseJump;
// Start is called before the first frame update
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
baseJump = Player.GetComponent<NejikoController>().speedJump;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
baseJump = Player.GetComponent<NejikoController>().getJump();
Player.GetComponent<NejikoController>().speedJump = baseJump * JumpUpRate;
StartCoroutine(JumpDown());
//Invoke("test", effectseconds);
}
}
IEnumerator JumpDown()
{
Debug.Log("A");
yield return new WaitForSeconds(effectseconds);
Debug.Log("B");
Player.GetComponent<NejikoController>().speedJump = baseJump;
Debug.Log("C");
}
private void test()
{
Debug.Log("D");
Player.GetComponent<NejikoController>().speedJump = baseJump;
Debug.Log("E");
}
}
自分で試したこと
・呼び出し前にTime.timeScale = 1;を実行
・Invokeでの実行
・effectsecondsを1.0fに置き換え
・別スクリプトで添付しているGoogleAds関係のコードを(一部)コメントアウト
0 likes