Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 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
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
Add comment · 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. $$anonymous$$aybe 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 : $$anonymous$$onoBehaviour {
     
     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
0

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
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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Instantiating a random dropped consumable item from many cloned objects 1 Answer

Instantiated GameObject's components are disabled 3 Answers

How can I make a game object follow an instantiated game object? 1 Answer

Auto Translate after cloning gameobject using Instantiate() 1 Answer

Position an instantiated prefab relative to its translation vector 2 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