Hi Guys
I know the title probably doesn’t explain stuff too well.
if anyone could help I’d be eternally grateful.
I’m currently making a hack and slash game, I’ve got to a point of doing the combat. Everything works with the one enemy but the moment I try to do it on an instatiated clone of my prefab the ‘clone’ acts as though it’s code has already been updated. For example.
I hit my first enemy > it dies
I hit the second enemy (clone) and none of my old logic applies to it.
I guess I am trying to find a way that makes it so every instance of a prefab behaviours in exactly the same way even if other gameobjects from the same prefab have been altered.
I understand basic amounts of how prefabs work but from my knowledge, each clone so run off a clone of each component, including the scripts attached to it?
I believe some of the issue comes from the way I edit the GameObjects my code is attached to.
Here is how I reference the prefabs.
My way of calling methods in my player script ( i.e. when I hit the enemy) - My enemy script is called “EnemyMovement”
void Update()
{
EnemyHitAndDeath = GameObject.FindObjectOfType();
Then this is called via my collider trigger
private void OnTriggerEnter2D(Collider2D other) {
if (isAttacking && !playerInAirAttackingEnemy && other.CompareTag(“Enemy”))
{
EnemyHitAndDeath.PlayerHitEnemy();
SwordHitEffects.PlaySwordHitAnimations(gameObject);
Debug.Log(“Attack1”);
Which then triggers this inside my enemy
public void PlayerHitEnemy()
{
myAnimController.SetBool(“IsHit”, true);
IHaveBeenHit = true;
if (!originalvelocityStored)
{
originalVelocity = rb2D.velocity;
originalvelocityStored = true;
}
rb2D.velocity = originalVelocity;
if (!StopAddingAcceleration)
{
if(transform.position.x > playerPos)
{
rb2D.AddForce(new Vector2(1f,0) * knockBackForce);
StopAddingAcceleration = true;
// This works!! //
//rb2D.AddForce(new Vector2 (0, VerticalHit), ForceMode2D.Impulse);
}
else
{
rb2D.AddForce(new Vector2(-1f,-0) * knockBackForce);
StopAddingAcceleration = true;
}
}
StartCoroutine(SwitchBackAfterHit());
}
And so on.
I just need it to be so every instance of the enemy operates independantly of the other one but for the life of me I can’t work it out. I was considering using multiple copies of my prefab in my assets but with the way I have created my code, I would have to find a way to trigger the method inside all of the enemies or more so trigger the method relevant to that script which seems like a bad way to do it.