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.
That's what I had originally, the loop and things were to try and fix it.
– SteamCyborgAdd... Debug.Log(gameObject.name); in the first line of the Attack function.. Does it say the game object or does it say the game object with (clone) after it.. if it does not say clone.. that means within your editor you put the prefab directly into the public game asset and when you spawn the projectile it is going to the original prefab and not the clones.
– cgklutts