• 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
Question by Slowmanrunning · Jan 22, 2014 at 07:18 PM · instantiatecloneasteroids

Instantiate(this.gameObject) causing crash

I'm creating a game similar to asteroids. Currently I have an asteroid with X max health, and upon reaching half of that, it reduces in size, sets max hp to current, then makes a copy of itself. When an asteroid's health hits <= 1, it's destroyed.

 public class AsteroidBreak : MonoBehaviour {
     
     public int startingHealth;
     private int health;
 
     void Start () {
         health = startingHealth;
     }
 
     void OnTriggerEnter (Collider other){
         Debug.Log("It's a hit!");
         if (other.tag == "Shot") {
             health--;
         }
         if (health <= 1) {
             Destroy(this.gameObject);
         }
         if (health <= startingHealth/2) {
             transform.localScale = transform.localScale/2;
             startingHealth = health;
             Instantiate(this.gameObject);
         }
     }
 }

However, I'm finding the game grinds to <5 fps or crashes upon destroying a copied asteroid. I tested it with

 //Instantiate(this.gameObject);

and the asteroids reduced in size and were destroyed fine. I went back and tested it more with cloning, and it only seems to be the copies' copies getting destroyed that causes a crash. (The original asteroid keeps moving in the same direction after shrinking, so it's easy to tell which are copies/originals).

I'm not really sure what the problem is. I was hoping to come up with some system that allows for dynamic asteroid size.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by QuestionAsker · Jan 22, 2014 at 08:35 PM

I can't help with the crash but i have some advice. A simple work around would be to attach a prefab to the script in the editor and instantiate that. i've never tried to instantiate anything like that however. I know sometimes static anything can cause small issues in mono, but i doubt that is the case. If i were you I'd restart your PC or at least Unity and if that fails, try the work around.

Comment

People who like this

0 Show 3 · 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
avatar image Slowmanrunning · Jan 22, 2014 at 10:55 PM 0
Share

Restarting the computer didn't help. Also, it seems I was wrong; killing the original does cause a crash. In fact, further testing seems like killing anything can cause a crash. They're just frequent, sporadic, and only happen when I have

 Instantiate(this.gameObject);

in the code. Maybe it has to do with killing something that was cloned? It happens when asteroids with no clones get destroyed as well. I'm frankly stumped.

Anyone have an alternative way of doing this?

avatar image Kiloblargh · Jan 23, 2014 at 12:39 AM 0
Share

You just got an alternative way of doing this.

A simple work around would be to attach a prefab to the script in the editor and instantiate that.

 public GameObject asteroid;

which you drag your asteroid prefab onto on the editor.

That's not an "alternative" way of doing it, it's the right way. I have never seem "'Instantiate (this.gameObject);'" before either but it does not surprise me that it would cause a crash.

avatar image Slowmanrunning · Jan 26, 2014 at 01:01 AM 0
Share

I figured it out. Adding "&& health > 1" to the third if statement fixed the random crashes. I guess sometimes the script hit that point before getting killed, and it was causing problems. Here's the fixed script:

 public class AsteroidBreak : MonoBehaviour {
     
     public int startingHealth;
     private int health;
 
     void Start () {
         health = startingHealth;
     }
 
     void OnTriggerEnter (Collider other){
         if (other.tag == "Shot") {
             health--;
         }
         if (health <= 1) {
             Destroy(gameObject);
         }
         if (health <= startingHealth/2 && health > 1) {
             transform.localScale = transform.localScale*0.6f;
             transform.rotation = Quaternion.Euler(0, 0, Random.Range(0, 359));
             startingHealth = health;
             Instantiate(gameObject);
         }
     }
 }
avatar image

Answer by Slowmanrunning · Jan 26, 2014 at 01:58 AM

I figured out the problem! Here's the complete working script:

 public class AsteroidBreak : MonoBehaviour {
     
     public int startingHealth;
     private int health;
 
     void Start () {
         health = startingHealth;
     }
 
     void OnTriggerEnter (Collider other){
         if (other.tag == "Shot") {
             health--;
         }
         if (health <= 1) {
             Destroy(gameObject);
         }
         if (health <= startingHealth/2 && health > 1) {
             transform.localScale = transform.localScale*0.6f;
             transform.rotation = Quaternion.Euler(0, 0, Random.Range(0, 359));
             startingHealth = health;
             Instantiate(gameObject);
         }
     }
 }

adding the "&& health > 1" condition to the last if statement seemed to fix the problem. I guess that sometimes the object ran the third if statement before the script was stopped.

Comment

People who like this

0 Show 0 · 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

20 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

Related Questions

Reference an Instance of an Object?C# 1 Answer

How to stop instantiate spawn ? 1 Answer

Instantiated Component not Cloning Parent Classes? 1 Answer

Variable being shared between clones 2 Answers

How can I destroy an specific clone already instantiated, when there's more than one? 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