• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ZarkonesGames · Jan 05, 2020 at 07:57 PM · instantiateunity 2dinstantiate prefabprefab-instance

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: alt text

Example 2: alt text

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.

bag1.jpg (90.3 kB)
bag2.jpg (97.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

220 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

All instantiated objects having same location, 0 Answers

I need help destroying a clone on function. 1 Answer

Moving a GameObject that was instantiated,Problem moving an instantiated Object. 0 Answers

Instantiate Causes errors when I spawn multiple particle prefabs at once. 1 Answer

How to spawn a prefab at a duplicate objects location 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges