- Home /
Scrolling combat text works on enemy. When I duplicate and have two enemies I get a problem.
I created scrolling combat text to float over an enemy when they are damaged by a bullet. When I duplicate the enemy everything works fine and I have two perfectly working enemies. The problem is when enemy2 gets hit, it displays the scrolling combat text over enemy1 and when enemy1 gets hit, it also displays the scrolling combat text over enemy1. The share the position of the scrolling combat text some how even though everything else is separate. They act like two individual enemies except for that part.
Please show the code, now there is no way for us to see what the issue is.
Answer by Geads · Nov 11, 2017 at 01:22 AM
EnemyCombatTextManager script
public class EnemyCombatTextManager : MonoBehaviour
{
public float speed;
public Vector3 direction;
public static EnemyCombatTextManager instance;
[SerializeField]
public GameObject textPrefab;
public RectTransform canvasTransform;
public float fadeTime;
public static EnemyCombatTextManager Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<EnemyCombatTextManager>();
}
return instance;
}
}
public void CreateText(Vector3 position, string text, Color color, bool crit)
{
GameObject sct = (GameObject)Instantiate(textPrefab, position, Quaternion.identity);
sct.transform.SetParent(canvasTransform, false);
sct.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
sct.GetComponent<CombatText>().Initialize(speed, direction, fadeTime, crit);
sct.GetComponent<Text>().text = text;
sct.GetComponent<Text>().color = color;
}
}
Where I call it in the enemy script
EnemyCombatTextManager.Instance.CreateText(transform.position, "-" + enemyrndDmg.ToString(), Color.red, false);
It works perfect for a single enemy. Once I duplicate the enemy so there is enemy1 and enemy2, the enemies both work perfect, but the scrolling combat text is only over enemy2 even if enemy 1 or enemy2 is getting damaged
public RectTransform canvasTransform; <-- never changes because you grab the same instance. So its likely still on the first enemy?
Perhaps you can pass it as a parameter ins$$anonymous$$d
public void CreateText(Vector3 position, string text, Color color, bool crit, RectTransform canvasTransform) {
}
sct.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
is this what you are referring to? This just sets the scale of the font to 1,1,1.
The text is showing on one canvas ins$$anonymous$$d of each duplicated enemies personal canvas
Your answer
![](https://koobas.hobune.stream/wayback/20220612142702im_/https://answers.unity.com/themes/thub/images/avi.jpg)