4

I'm trying to make a script so that on a button press the particle system changes to a certain color, it all works fine apart from changing the particle color, when I try it comes up with this error:

NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour
{
    public int MovementDirection = 0;
    public int State = 0;

    public GameObject attackOrb; //The prefab for our attack hitbox
    public Transform Player;        //Where the player is

    public float R = 0.0F;
    public float G = 0.0F;
    public float B = 0.0F;
    public float A = 1.0F;

    private ParticleSystem attackEffect;

    // Start is called before the first frame update
    void Start()
    {
        attackEffect = gameObject.GetComponent<ParticleSystem>();
    }

    // Update is called once per frame
    void Update()
    {
        var main = attackEffect.main;
        main.startColor = new Color(R, G, B, A);

        if (Input.GetKeyDown(KeyCode.Keypad1)) State = 1;
        if (Input.GetKeyDown(KeyCode.Keypad2)) State = 2;
        if (Input.GetKeyDown(KeyCode.Keypad3)) State = 3;
        if (Input.GetKeyDown(KeyCode.Keypad4)) State = 4;
        if (Input.GetKeyDown(KeyCode.Keypad5)) State = 5;
        if (Input.GetKeyDown(KeyCode.Keypad6)) State = 6;


        switch(State)
        {
            case 0:
                GetComponent<Renderer>().material.color = new Color(1f, 0.5f, 0.5f, 0.5f);
                R = 1f;
                G = 0.5f;
                B = 0.5f;
                A = 0.5f;
                break;

It's meant to come out as the R, G, B, A colors but instead returns that error. why does it return this and how would I get around fixing this?

Full error:

NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
UnityEngine.ParticleSystem+MainModule.set_startColor (UnityEngine.ParticleSystem+MinMaxGradient value) (at C:/buildslave/unity/build/artifacts/generated/bindings_old/common/ParticleSystem/ParticleSystemBindings.gen.cs:50)
Attack.Update () (at Assets/Script/Attack.cs:30)
Mistyisty
  • 113
  • 1
  • 1
  • 6

2 Answers2

2

This is how to change the startColor of a Particle System in Unity3D:

    var main = particleSystem.main;
    main.startColor = new ParticleSystem.MinMaxGradient(new Color(R, G, B, A));

If you are wondering why check the type of ParticleSystem.MainModule.startColor. It is not of type Color but ParticleSystem.MinMaxGradient.

Dave
  • 2,684
  • 2
  • 21
  • 38
2

You probably created a gameobject and added the particle system component.

to fix this create a particle system from the add menu not a gameobject. you can copy the transform and the particle data from the old one and paste them into the newly created particle system. its probably a unity bug (using 2019.1.14f1)

And you can access it like this... its deprecated but its the only thing that works, but you can try the others maybe they will work as a result of the above fix.

particleSystem.startColor = Color.white - (whatever color);