I have tried everything
my prefabs don’t get counted in score when they get killed…
THIS IS 2D AND CSharp language
here’s the script- this script WORKS with gameobjects in the scene inspector already but doesn’t work with prefabs!!
this does work on in scene gameobjects ,but not on prefabs, with prefabs the inspector for them wont let me put level manager" in the slot or drag it there…
why is this happening and please someone help me or post corrected script or tell me in detail what to do and what im doing wrong
using UnityEngine;
using System.Collections;
public class playercscorecard : MonoBehaviour
{
public int increaseAmount; // how much will the count increase when the player hits enemy, one time only
public LevelManager levelManager; // the instance of the LevelManager class. The empty game object which is attached with LevelManager script. Drag and drop the emptyGO.
public float waitTime; // how much time is it going to take for the enemy to be destroyed after hit.
private bool canStartCoroutine; // boolean to handle simple " one time only " logic
void Start()
{
canStartCoroutine = true;
}
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Enemy")
{
if(canStartCoroutine)
{
canStartCoroutine = false; // after one hit, boolean is made false immediately to prevent it from updating again.
StartCoroutine(WaitToDestroy(waitTime));
}
}
}
IEnumerator WaitToDestroy(float time)
{
levelManager.killCount += increaseAmount; // increase the kill count by increaseAmount value
levelManager.SetKillCount(); // refresh the kill count
yield return new WaitForSeconds(time); // wait for the desired time
Destroy(gameObject); // destroy the gameobject itself.
}
}