- Home /
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.
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.
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?
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.
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);
}
}
}
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.
Your answer
Follow this Question
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