Problem with waitforsecond and IEnumerator
Hi. I'm trying to place a block ever 0.15 second when i,M holding mouse button down. it Works perfectly. However, when i try to do the same thing to destroy them (every 0.3 second) it waits 0.3 second, then destroy one every frame. How can i fix this ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class placeBlock : MonoBehaviour
{
public GameObject cube;
public Collider up;
public Collider bottom;
public Collider right;
public Collider left;
public Collider front;
public Collider back;
if(Input.GetMouseButton(0))
{StartCoroutine(MyMethod());}
if(Input.GetMouseButton(1))
{StartCoroutine(MyMethod2());}
}
void Place2(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo))
{
Destroy(hitInfo.transform.parent.gameObject);}
}
void Place(){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider == up){
Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y + 1,this.transform.position.z), Quaternion.identity);}
if (hitInfo.collider == bottom){
Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y - 1,this.transform.position.z), Quaternion.identity);}
if (hitInfo.collider == right){
Instantiate(cube,new Vector3(this.transform.position.x - 1,this.transform.position.y,this.transform.position.z), Quaternion.identity);}
if (hitInfo.collider == left){
Instantiate(cube,new Vector3(this.transform.position.x + 1,this.transform.position.y,this.transform.position.z), Quaternion.identity);}
if (hitInfo.collider == front){
Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y,this.transform.position.z - 1), Quaternion.identity);}
if (hitInfo.collider == back){
Instantiate(cube,new Vector3(this.transform.position.x,this.transform.position.y,this.transform.position.z + 1), Quaternion.identity);}
}}
IEnumerator MyMethod(){
yield return new WaitForSeconds(0.15F);
Place();
}
IEnumerator MyMethod2(){
yield return new WaitForSeconds(0.3F);
Place2();
}}
I think this is not good..
if(Input.Get$$anonymous$$ouseButton(0))
{StartCoroutine($$anonymous$$y$$anonymous$$ethod());}
you are starting a new coroutine every frame that the button is pressed... So you are simultaneoulsy running the $$anonymous$$y$$anonymous$$ethod() multiple times... so you are calling Place() each frame! the ield return new WaitForSeconds(0.15F); is just delaying the action...Thats not good.
$$anonymous$$aybe is better to run the corutine one, looping every 0.15f, and inside the corutine detect if mouse is pressed.
You should watch some tutorials and learn more. Commence by something simple, domain it, and when you fully understand the corutine process, make it more complex.
Bye!
This way will have only 1 $$anonymous$$y$$anonymous$$ethod running.
Answer by Pangamini · May 15, 2019 at 09:13 AM
Well, you are spawning coroutines every frame. Each coroutine waits the given time, then creates / destroys the thing. They don't wait for each other, they are not linked in any way. How about, instead of using coroutines, you just remember when's the last time you created / destroyed something. Then, if the mouse is held down, you check for the time, and only if the time difference is big enough, you perform the action (and also write the new time)
Your answer
Follow this Question
Related Questions
While loop freezes game for 3 seconds then unfreezes (not infinite loop!) c# 1 Answer
if (score >= 10) Executes endlessly. How to prevent it? 1 Answer
Unity crashes when using while loop 0 Answers
What is wrong with my script to change shader on all GameObjects? 1 Answer
(c#) - For/Foreach loop skips indexes, jumping to different indexes at random?? 0 Answers