- Home /
I want to active my coins again after few minutes ?
My player collides with the coin, and it the coin is deactivated in the hierarchy? But after a few seconds, I want to my coin should be activated again? I have done the code, but to coins are not reactivating? Please help me with this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Coin_Score : MonoBehaviour {
public Text score;
public GameObject coin;
private int scoreupdate;
public bool cointrue = true;
// Use this for initialization
void Start () {
score.text = "0" ;
scoreupdate = 0 ;
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Coins"))
{
other.gameObject.SetActive(false);
scoreupdate = scoreupdate + 5 ;
cointrue = false;
SetCountText();
}
}
void SetCountText()
{
score.text = scoreupdate.ToString();
}
void Lateupdate()
{
StartCoroutine(activecoin());
}
IEnumerator activecoin()
{
if (cointrue == false)
{
if (GameObject.FindGameObjectWithTag("Coins").activeInHierarchy == false)
{
yield return new WaitForSeconds(5.0f);
GameObject.FindGameObjectWithTag("Coins").SetActive(true);
cointrue = true;
}
}
}
}
Answer by swanne · Jul 12, 2018 at 07:36 AM
I would suggest rather than activating and deactivating your coins, you should spawn and destroy your coins. A coin will be a prefab and have a script with OnCollisionEnter() Destroy this.gameObject - if the other collider tag is "Player". Then on the playerController script, you could have a coroutine which spawns the coin prefab after 5 seconds at the position of where is what collected by the player, again in an OnCollisionEnter() function on the playerController script.
Answer by mayur7garg · Jul 12, 2018 at 09:11 AM
@sa201997 Functions like Start(), Update(), LateUpdate(), etc. are MonoBehavior components and are called only for objects active in hierarchy. So as soon as the coin goes inactive, LateUpdate() of this script stops being called. Consequently, the coroutine is never started. This can be resolved by calling this coroutine from another script which is attached to a gameObject which remains active. And by the way, it is LateUpdate(), not Lateupdate(). Happy to help!
Answer by AdityaViaxor · Jul 12, 2018 at 11:57 AM
Have you placed few coins in scene manually and when you collide them they set inactive if this is true then the problem with your code is inside activate coin you check for object in hierarchy with tag "coins" is active or inactive and suppose there are 4 to 5 coin with tag coins the you if condition (if(GameObject.FindGameObjectWithTag("Coins").activeInHierarchy == false)) get false and compiler goes out of condition.
try using object pooling for stuff like this, just google for object pooling in unity and you will find soloution. make a pool of coins in one script and invoke them in another script like game manager and set inactive when collide with player.
calling activeCoin from update is very bad practice in future if you game grow these things will cause lagging. try not to use update or use as less as possible.
thank you
Your answer
![](https://koobas.hobune.stream/wayback/20220612170542im_/https://answers.unity.com/themes/thub/images/avi.jpg)