When the player touches a coin, the score goes up but the coin doesn’t disappear.
Here is the CoinScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinScript : MonoBehaviour {
private LevelManager gameLevelManager;
public int coinValue;
// Use this for initialization
void Start () {
gameLevelManager = FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update(){}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player")
{
gameLevelManager.AddCoins(coinValue);
Destroy (gameObject);
}
}
}
Here is the LevelManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour {
public float respawnDelay;
public PlayerController gamePlayer;
public int coins;
public Text coinText;
// Use this for initialization
void Start () {
gamePlayer = FindObjectOfType<PlayerController>();
coinText.text = "Coins: " + coins;
}
// Update is called once per frame
void Update () {
}
public void Respawn(){
StartCoroutine ("RespawnCoroutine");
}
public IEnumerator RespawnCoroutine(){
gamePlayer.gameObject.SetActive (false);
yield return new WaitForSeconds (respawnDelay);
gamePlayer.transform.position = gamePlayer.respawnPoint;
gamePlayer.gameObject.SetActive (true);
}
public void AddCoins(int numberOfCoins){
coins += numberOfCoins;
coinText.text = "Coins: " + coins;
}
}
I am new to all of this.,When I run the game, the score goes up when the player touches a coin. But for some reason, the player goes through the coin and the coin stays on screen.
Here is my CoinScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinScript : MonoBehaviour {
private LevelManager gameLevelManager;
public int coinValue;
// Use this for initialization
void Start () {
gameLevelManager = FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update(){}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player")
{
gameLevelManager.AddCoins(coinValue);
Destroy (gameObject);
}
}
}
Here is my LevelManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour {
public float respawnDelay;
public PlayerController gamePlayer;
public int coins;
public Text coinText;
// Use this for initialization
void Start () {
gamePlayer = FindObjectOfType<PlayerController>();
coinText.text = "Coins: " + coins;
}
// Update is called once per frame
void Update () {
}
public void Respawn(){
StartCoroutine ("RespawnCoroutine");
}
public IEnumerator RespawnCoroutine(){
gamePlayer.gameObject.SetActive (false);
yield return new WaitForSeconds (respawnDelay);
gamePlayer.transform.position = gamePlayer.respawnPoint;
gamePlayer.gameObject.SetActive (true);
}
public void AddCoins(int numberOfCoins){
coins += numberOfCoins;
coinText.text = "Coins: " + coins;
}
}