I’m instantiating the prefab “Pinner”, and when it attacks it instantiates a projectile.
These projectiles all instantiate from the original Pinner, not the new clone.

I’ve searched for hours now and I can’t find a solution to what seems like a VERY basic problem, so I’m thinking it’s something due to my inexperience and not a complex issue at all.

Please send help >.>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pinner : MonoBehaviour
{
public float damage;

private GameObject player;
public GameObject projectile;

private AudioSource pew;
private CameraShake Shake;
public Animator cameraShake;
public GameObject deathParticle;
public Transform attackPos;
public LayerMask Enemies;
public Transform shootPoint;
public float attackRange;
private Transform[] children;

// Start is called before the first frame update
void Start()
{

    children = GetComponentsInChildren<Transform>();
    foreach (Transform child in children)
    {
        if (child.gameObject.name == "ShootPoint")
        {
            shootPoint = child;
        }
    }

    pew = GetComponent<AudioSource>();
    player = GameObject.FindGameObjectWithTag("Player");
    Shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<CameraShake>();
}

// Update is called once per frame
void Update()
{
    
}

void LateUpdate()
{
    if (transform.position.x - player.transform.position.x < 0)
    {
        transform.localScale = new Vector3(-0.3f, transform.localScale.y, 1.0f);
    }
    else if (transform.position.x - player.transform.position.x >= 0)
    {
        transform.localScale = new Vector3(0.3f, transform.localScale.y, 1.0f);
    }

}

void OnTriggerEnter2D(Collider2D other)
{

    Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, Enemies);
    if (enemiesToDamage.Length != 0)
    {
        for (int i = 0; i < enemiesToDamage.Length; i++)
        {
            //FindObjectOfType<AudioManager>().Play("HitFast");

            if (other.gameObject.layer == 10)
            {
                Shake.CamShake();
                Instantiate(deathParticle, transform.position, Quaternion.identity);
                enemiesToDamage*.GetComponent<Player>().Kill("LittlePin");*

Destroy(this.gameObject);
}
else if (other.gameObject.layer == 8)
{
enemiesToDamage*.GetComponent().TakeDamage(10);*
}
}
}
}
public void Attack()
{
print(“boo”);
_ = Instantiate(projectile, shootPoint.position, shootPoint.rotation);
pew.Play();
}
}
,So, I’m new to unity and after hours of research, I can’t seem to fix my problem: my cloned game objects are all firing from the first’s body.
I spawn multiple copies of the same prefab in using a spawner and they all fire from the original, and if I destroy the original from one of the clones.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pinner : MonoBehaviour
{
public float damage;
private GameObject player;
public GameObject projectile;
private AudioSource pew;
private CameraShake Shake;
public Animator cameraShake;
public GameObject deathParticle;
public Transform attackPos;
public LayerMask Enemies;
public Transform shootPoint;
public float attackRange;
private Transform[] children;

// Start is called before the first frame update
void Start()
{
children = GetComponentsInChildren();
foreach (Transform child in children)
{
if (child.gameObject.name == “ShootPoint”)
{
shootPoint = child;
}
}
pew = GetComponent();
player = GameObject.FindGameObjectWithTag(“Player”);
Shake = GameObject.FindGameObjectWithTag(“ScreenShake”).GetComponent();
}
// Update is called once per frame
void Update()
{

}
void LateUpdate()
{
if (transform.position.x - player.transform.position.x < 0)
{
transform.localScale = new Vector3(-0.3f, transform.localScale.y, 1.0f);
}
else if (transform.position.x - player.transform.position.x >= 0)
{
transform.localScale = new Vector3(0.3f, transform.localScale.y, 1.0f);
}
}
void OnTriggerEnter2D(Collider2D other)
{
Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, Enemies);
if (enemiesToDamage.Length != 0)
{
for (int i = 0; i < enemiesToDamage.Length; i++)
{
//FindObjectOfType().Play(“HitFast”);
if (other.gameObject.layer == 10)
{
Shake.CamShake();
Instantiate(deathParticle, transform.position, Quaternion.identity);
enemiesToDamage*.GetComponent().Kill(“LittlePin”);*
Destroy(this.gameObject);
}
else if (other.gameObject.layer == 8)
{
enemiesToDamage*.GetComponent().TakeDamage(10);*
}
}
}
}
public void Attack()
{
print(“boo”);
_ = Instantiate(projectile, shootPoint.position, shootPoint.rotation);
pew.Play();
}
}
Right after the void Start the “children = GetComponentsInChildren();” part was my attempt at trying to get the clone to refer to itself, to no avail.

Can I see the code where you spawn this Pinner class?
I’m guessing its all spawning under the same parent… so when you loop through the transforms in your start function it is somehow assigning shoot point to the same game object. You are using a loop when a loop is not necessary.

This

 children = GetComponentsInChildren<Transform>();
 foreach (Transform child in children)
 {
     if (child.gameObject.name == "ShootPoint")
     {
         shootPoint = child;
     }

 }

Should be this

 shootPoint = gameObject.transform.Find("ShootPoint");

“I spawn multiple copies of the same prefab in using a spawner and they all fire from the original, and if I destroy the original from one of the clones.”
So they all fire from the original, and once you despawn the original they all start firing from some other instance? It seems to me like its not a problem with your spawn point, but maybe with how you are calling Attack() if this is true. Where do you call this function from?

Solved!
In my StateMachineBehavior which calls the attack from the MonoBehaviour during the attack animation, I replaced the public Pinner pinner; with a private one:
pinner = animator.GetComponentInParent();
Thank you everyone for your help <3