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)