I've created a prefab for coin. The Scenario is like that : When player collides with coin, the coin will move towards coin image(coin image is one which is displayed in header for information purpose). In a prefab, there is random number of coins attached under one empty gameobject. and one script is attached with each coin to move towards static coin image. I am posting the snippet of that script.
public bool isMove;
void Update(){
if(isMove){
transform.position = Vector3.MoveTowards (transform.position, GameObject.FindGameObjectWithTag ("HeaderCoin").transform.position,Time.deltaTime*30);
}
}
void OnTriggerEnter2D(Collider2D target){
if (target.tag == "Player") {
isMove = true;
PlayerOfGame.instance.PlayCoins+=1;
GamePlayController.instance.SetPlayCoins (PlayerOfGame.instance.PlayCoins);
PlayerOfGame.instance.coins+=1;
StartCoroutine (HideCoin ());
}
}
IEnumerator HideCoin()
{
yield return new WaitForSeconds (0.5f);
transform.gameObject.SetActive (false);
}
when playing game, it gets hang after sometime. I think the problem is with prefab. So what should I change to make my game more smooth.
transform.gameObject..SetActive(false), I would destroy them withDestroy(gameObject). When you just deactivate them, they keep existing in an inactive state, which clutters up your scene and keeps consuming resources. \$\endgroup\$