- Home /
Help! prefabs don't get counted in score when they get killed
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.
}
}
the code works on gameobjects in the scene just not on prefabs??
just a check you set increaseAmount to something besides zero on the prefab correct?
Answer by badatnames16 · Mar 09, 2015 at 12:45 AM
I think you need to add a line in the "WaitToDestroy" IEnumertator to set the canStartCoroutine bool to true again.
if I bring the prefab into the scene by dragging and then drag level manager to the prefab it will count it but some reason otherwise it wont
are you applying the change when you put the level manager into it?
yes I have the prefabs tagged
prefabs are stored in a different area than gameobjects
when the prefabs are in their "prefabs" location folder I cannot put the level manager on them it wont let me drag anything on them in that area its just saying none. but when I drag the prefab to the scene I can then put the level manager on them and it lets me
Your answer
Follow this Question
Related Questions
Best Way To Reference Instantiated Prefabs with Each Other. 0 Answers
Memory Game Question 1 Answer
How to get a component from a prefab. 5 Answers
"Change" Prefab Component Attributes 0 Answers
Unity Programmatically Link Prefab 2 Answers