- Home /
Script is only working on original object
Whenever I try to duplicate the object, it will not take away health from the bar, but the original one will.
{
healthBar.transform.position = new Vector2(healthPos, 30);
healthBar.GetComponent<RectTransform>().sizeDelta = new Vector2(healthLength, 30);
if (decreasingHealth == true)
{
if (damageAmount >= hitValue)
{
decreasingHealth = false;
damageAmount = 0;
}
else
{
damageAmount += 0.5f;
healthLength -= 0.5f;
healthPos -= 0.25f;
Debug.Log("Damage Taken");
}
}
if (increasingHealth == true)
{
if (damageAmount >= hitValue * 2)
{
increasingHealth = false;
damageAmount = 0;
}
else
{
damageAmount += 0.5f;
healthLength += 0.5f;
healthPos += 0.25f;
}
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
attackTrigger = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
attackTrigger = false;
}
}
IEnumerator InflictDamage()
{
isAttacking = true;
yield return new WaitForSeconds(1.1f);
GlobalHealth3.currentHealth -= 5;
decreasingHealth = true;
yield return new WaitForSeconds(0.9f);
isAttacking = false;
}
}
I've been browsing for hours on ways to fix this and I can not seem to find an answer to why this has been happening. any ideas?
Answer by Fragsteel · Feb 19, 2020 at 11:05 PM
Can you provide a bit more context? For instance, how you are duplicating the object, and where this script exists? It's difficult to understand exactly what your problem is from just looking at the script.
The rest of this script may help, too.
Screenshots (or even a video!) would be perfect.
Also, if the copy is supposed to be losing health, do you see the original version lose health by any chance? Wondering if it's got a bad reference.
The health bar i'm trying to have lose is the player's. So what I do is if the AI enemy attacks the player, they lose health. But that only happens with the first ai that got the script.
I am duplicating them by using CTRL+D The script exists within the enemy object ai.
Answer by UnityProjec · Feb 21, 2020 at 08:55 PM
Okay, so I discovered that it removes health within the script of the healthbar, where it should be going down and it says it is within the script. Yet on the actual gameobject for the healthbar within the canvas it is not changing on attack from any entity other than the original enemy ai.
Your answer
Follow this Question
Related Questions
Another function for activating a different script 0 Answers
Ontrigger not working with npc 0 Answers
I am trying to access a variable from another script. 1 Answer
Stop enemies from grouping up 1 Answer
Trigger Sound more than Once 0 Answers