Hi, I’m trying to disable and enable a Particle System. At the docs I read the following:
ParticleSystem ps = GetComponent<ParticleSystem>();
var em = ps.emission;
em.enabled = true;
I have implemented this code in my script. The particle system and following script is attached to my player. When the player jumps and lands the particle system should play dust particles on landing and on a OnTriggerEnter event that is attached to a floor object. The particle system has a collider. At the moment the code below isn’t working. The particle system still starts emitting on start.
I’m getting this error:
“NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance”
Anyone any suggestion or can see what I did wrong? Still new to Unity.
The OnTriggerEnter event is listed below in the code where the particles get activated:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerJumpScript3D : MonoBehaviour {
public static PlayerJumpScript3D instance;
private Rigidbody myBody;
private Animator anim;
public ParticleSystem dustParticle;
[SerializeField]
private float forceX, forceY;
private float tresholdX =7f;
private float tresholdY = 14f;
//Treshold was X 7f en Y 14f
private bool setPower, didJump;
private bool isGrounded = true;
private Slider powerBar;
private float powerBarTreshold = 10f;
private float powerBarValue = 0f;
void Awake ()
{
transform.Rotate(0,90,0);
MakeInstance();
Initialize();
SetPower();
}
void Update ()
{
SetPower();
}
void Initialize ()
{
powerBar = GameObject.Find ("Power Bar").GetComponent<Slider>();
myBody = GetComponent<Rigidbody> ();
anim = GetComponent<Animator> ();
ParticleSystem dustParticle = GetComponent<ParticleSystem>();
var dustPrtcl = dustParticle.emission;
dustPrtcl.enabled = false;
powerBar.minValue = 0f;
powerBar.maxValue = 10f;
powerBar.value = powerBarValue;
}
void MakeInstance ()
{
if(instance == null)
instance = this;
}
void SetPower ()
{
if (setPower) {
forceX += tresholdX * Time.deltaTime;
forceY += tresholdY * Time.deltaTime;
//if(forceX > 6.5f)
//forceX = 6.5f;
//if(forceY > 13.5f)
//forceY = 13.5f;
if(forceX > 6.5f)
forceX = 6.5f;
if(forceY > 13.5f)
forceY = 13.5f;
powerBarValue += powerBarTreshold * Time.deltaTime;
powerBar.value = powerBarValue;
}
}
public void SetPower (bool setPower)
{
this.setPower = setPower;
if (!setPower) {
Jump();
}
}
void Jump ()
{
if (isGrounded)
{
myBody.velocity = new Vector3 (forceX, forceY);
forceX = forceY = 0f;
didJump = true;
anim.SetBool("Jump", didJump);
powerBarValue = 0f;
powerBar.value = powerBarValue;
}
}
void OnTriggerEnter (Collider target)
{
if (didJump) {
didJump = false;
anim.SetBool("Jump", didJump);
if (target.tag == "Platform") {
isGrounded = true;
if (GameManager.instance != null) {
GameManager.instance.CreateNewPlatformAndLerp (target.transform.position.x);
var dustPrtcl = dustParticle.emission;
dustPrtcl.enabled = true;
}
if (ScoreManager.instance != null /*&& forceX > 0.1f && forceY > 0.1f*/) {
ScoreManager.instance.IncrementScore();
ScoreManager.instance.HighScore ();
}
}
}
if (target.tag == "Dead") {
if (GameOverManager.instance != null) {
GameOverManager.instance.GameOverShowpanel();
}
Destroy(gameObject);
}
}
private void OnTriggerExit(Collider target)
{
if (target.tag == "Platform")
isGrounded = false;
}
}