- Home /
Particle gets destroyed, and gameObject can no longer referenced = no more particles spawning
In a beginners tutorial, I am making death particles appear on death, then a Destroy script cleans them up. They work well for as long as the lifetime that I give them- that is, when the lifetime of the first spawned emitter is over, the game can no longer find a gameObject to reference... so I get no more particles. I want to explode basically every time I run into the enemy, while still cleaning up particle systems after use.
Here's my script:
using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float moveSpeed; public GameObject deathParticles; private float maxSpeed = 5f; private Vector3 input;
private Vector3 spawn;
// Use this for initialization
void Start () {
spawn = transform.position;
}
// Update is called once per frame
void Update() {
if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)
{
input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
GetComponent<Rigidbody>().AddForce(input * moveSpeed);
}
}
void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Enemy")
{
Instantiate(deathParticles, transform.position, Quaternion.identity);
transform.position = spawn;
print("You dead");
}
}
//rigidbody.Addforce(input * moveSpeed);
}
And my destroy script is:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Destroy : MonoBehaviour { public float lifetime=0;
// Use this for initialization
void Start() {
Destroy(gameObject, lifetime);
}
}
// Update is called once per frame
//void Update () {
//}
,Hi there. I'm quite new at this, following a tutorial you've probably seen before. Right now I have a particle fab that is set to spawn sparks whenever the player dies. It works, spawning many clones, until the Destroy script I have removes the first emitter. Suddenly it's saying it can't reference 'GameObject' anymore (no duh)- the original particle emitter, and therefore appears not to be able to Instantiate anymore.
I can set my lifetime to very long, and spawn clones only during the lifetime of that initial emitter.
What am I doin' wrong?
using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float moveSpeed; public GameObject deathParticles; private float maxSpeed = 5f; private Vector3 input;
private Vector3 spawn;
// Use this for initialization
void Start () {
spawn = transform.position;
}
// Update is called once per frame
void Update() {
if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)
{
input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
GetComponent<Rigidbody>().AddForce(input * moveSpeed);
}
}
void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Enemy")
{
Instantiate(deathParticles, transform.position, Quaternion.identity);
transform.position = spawn;
print("You dead");
}
}
//rigidbody.Addforce(input * moveSpeed);
}
And my Destroy script is:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Destroy : MonoBehaviour { public float lifetime=0;
// Use this for initialization
void Start() {
Destroy(gameObject, lifetime);
}
}
// Update is called once per frame
//void Update () {
//}
Your answer
Follow this Question
Related Questions
Player Guide (particles) destroy collectable 0 Answers
Particles disappear when game object is destroyed... 7 Answers
Particles with collisions still passing through 0 Answers
autodesrtuct property of particle system in 3.5.0f5 3 Answers
How to get rid of (cloned) particles after an explosion 2 Answers