Instantiating two prefabs instead one (Bug)
Problem that is busting my nerves in days. Could not figure it out. So here is the problem, player is shooting at objects and if they have script Dest.cs it manages how they die. Player script creates a Bullet prefab that handles collisions by calling Dest.cs -> damage(float); that checks if health is lower than 0, then it "dies", if not, just decrease health variable. In Dest.cs; print("DEAD") and print("OBJS_TO_CREATE"); is printed once or twice (but in example number two).
Example 1:
Example 2:
But example two is made becouse of two colliders, or at least I think so, but example number one happens even with NPCs, but they have a single collider. Car transform is tagged with "Car" and NPC is tagged with "Dest".
Bullet.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public int s;
public Transform player;
public bool is_unique;
public float damage;
private Vector3 dir;
public bool shoot_forward;
public Transform panic;
public bool police_bullet;
private void Start() {
if (!is_unique) StartCoroutine(delete());
dir = player.GetComponent<PlayerController>().get_direction();
if (!police_bullet) Instantiate(panic, transform.position, transform.rotation);
}
private void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.CompareTag("Dest") && !police_bullet) {
other.GetComponent<Dest>().damage(damage);
Destroy(gameObject);
}
if (other.gameObject.CompareTag("Car")) {
other.GetComponent<Dest>().damage(damage);
Destroy(gameObject);
}
if (other.gameObject.CompareTag("Static")) {
Destroy(gameObject);
}
if (other.gameObject.CompareTag("PlayerHitbox") && police_bullet) {
player.GetComponent<PlayerController>().damage(damage);
Destroy(gameObject);
}
if (other.gameObject.CompareTag("Heli") && police_bullet) {
other.GetComponent<Dest>().damage(damage);
Destroy(gameObject);
}
}
private void FixedUpdate() {
if (shoot_forward) {
if (police_bullet) {
transform.position += transform.forward * Time.deltaTime * s;
} else {
transform.position += transform.up * Time.deltaTime * s;
}
} else {
transform.position += dir * Time.deltaTime * s;
}
}
private IEnumerator delete() {
yield return new WaitForSeconds(1.0f);
Destroy(this.gameObject);
}
}
Dest.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dest : MonoBehaviour {
public float health;
private GameObject play_cam;
public bool shake_on_dead;
public List<Transform> create_when_dead;
private void Start() {
play_cam = GameObject.FindWithTag("MainCamera");
}
public void damage(float amount) {
if (health <= 0) {
print("DEAD");
Destroy(this.gameObject);
if (shake_on_dead) play_cam.GetComponent<ScreenShake>().shake();
if (create_when_dead.Count != 0) {
print("OBJS_TO_CREATE");
GameObject obj = new GameObject();
Instantiate(obj, transform.position, transform.rotation);
foreach (Transform x in create_when_dead) {
GameObject obj_child = Instantiate(x.gameObject, transform.position, transform.rotation) as GameObject;
obj_child.transform.parent = obj.transform;
}
}
} else health -= amount;
}
}
Unity Version - 2019.2.13f1 Personal, OS: Windows 10, Late 2019 Build.
Your answer
Follow this Question
Related Questions
All instantiated objects having same location, 0 Answers
Instantiated object not in right position 1 Answer
Instantiated prefabs acting different than those dragged onto scene 0 Answers
Assign variables on Instantiation? 0 Answers
How do I properly deal with Internal_CloneSingle can only be called from the main thread 0 Answers