• 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 /
avatar image
0
Question by unity_71287E21DC7740737B97 · Jan 24 at 08:38 PM · unity 2d

Can't clone if parent is destroyed

How can I change this script so that the parent can still be cloned after it is destroyed?

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

public class EnemySpawner : MonoBehaviour { [SerializeField] private GameObject slimePrefab;

 [SerializeField]
 private float slimeInterval = 3.5f;

 // Start is called before the first frame update
 void Start()
 {
     StartCoroutine(spawnEnemy(slimeInterval, slimePrefab));
 }

 // Update is called once per frame
 private IEnumerator spawnEnemy(float interval, GameObject enemy) {
     yield return new WaitForSeconds(interval);
     GameObject newEnemy = Instantiate(enemy, new Vector3(Random.Range(-3f, 3), Random.Range(-5f, 5f), 0), Quaternion.identity);
     StartCoroutine(spawnEnemy(interval, enemy)); 
  
     }



Comment
Add comment · Show 5
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
avatar image Captain_Pineapple · Jan 24 at 08:40 PM 0
Share

which parent? nothing is getting destroyed here. This needs more information.

avatar image unity_71287E21DC7740737B97 Captain_Pineapple · Jan 25 at 04:51 PM 0
Share

The object gets destroyed in play. It's a wave based PVE where enemies are spawned in randomly and the player swings their sword at them, destroying the game object.

avatar image nce79 · Jan 25 at 05:01 PM 1
Share

is the slimeprefab being referenced from the scene or in an asset folder. If the scene then thats the issue

avatar image unity_71287E21DC7740737B97 nce79 · Jan 25 at 05:29 PM 0
Share

I dragged the slime prefab directly from the asset folder into the Enemy Spawner parameter, and I am getting the same error.

NullReferenceException: Object reference not set to an instance of an object PlayerHealth.TakeDamage (System.Int32 amount, System.Single dmg) (at Assets/Scripts/PlayerHealth.cs:23) EnemyDamage.OnTriggerEnter2D (UnityEngine.Collider2D collider) (at Assets/Scripts/EnemyDamage.cs:25)

PlayerHealth Script:

     public class PlayerHealth : MonoBehaviour
     {
         public Image healthBar;
         public int realth;
         public int maxRealth = 100;
         public float healthAmount = 100f;
     
         // Start is called before the first frame update
         void Start()
         {
             realth = maxRealth;
         }
     // Update is called once per frame
     public void TakeDamage(int amount, float dmg) {
         realth -= amount;
         healthAmount -= dmg;
         healthBar.fillAmount = healthAmount / 100f;
         if (realth <= 0) {
             Destroy(gameObject);
         }
     }
 }

EnemyDamage Script:

 public class EnemyDamage : MonoBehaviour
 {
     public PlayerHealth playerHealth;
     public int damage = 1;
     public float dmg = 1;
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
     private void OnTriggerEnter2D(Collider2D collider)
     {
         if (collider.gameObject.tag == "Player")
         {
             playerHealth.TakeDamage(damage, dmg);
         }
     }
 }



avatar image nce79 · Jan 25 at 05:45 PM 0
Share

so the playerhealth is no longer being referenced by the enemey damage because you destroy the object with the playerhealth script. you could find by type or a tag ex.` PlayerHealth playerhealth = FindObjectOfType();`

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RobinHut3 · Jan 27 at 11:28 PM

Your GameObject enemy is supposed to be a Prefab and as far as I understand you somehow delete thate GameObject "enemy" and than cant Instaniate it anymore. Prefabs are never Destroyed in the first place they are just a blueprint which you store in your assets. If I understood correctly just watch I video or read some documenation about Prefabs.

Comment
Add comment · Share
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

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

172 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

Related Questions

In Unity 2d is it possible to change which sprite sheet a player is using? 0 Answers

How to change sprite only on click or during shoot state? 1 Answer

increase speed every spawn wave 0 Answers

When I build my 2D game and set it to fullscreen, the resolution looks off 0 Answers

Magnet effect to draw coins,Magnetic effect to draw coins 5 Answers


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