我正在尝试制作一个脚本,以便按下按钮时粒子系统会更改为某种颜色,除了更改粒子颜色外,它都可以正常工作,当我尝试它时会出现这个错误:
NullReferenceException:不要创建自己的模块实例,从ParticleSystem实例获取它们
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;
它意味着以R,G,B,A颜色出现,而是返回该错误。为什么它会返回这个,我将如何解决这个问题呢?
完整错误:
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)
这是如何在Unity3D中更改粒子系统的startColor
:
var main = particleSystem.main;
main.startColor = new ParticleSystem.MinMaxGradient(new Color(R, G, B, A));
如果你想知道为什么检查ParticleSystem.MainModule.startColor
的类型。它不是Color
类型,而是ParticleSystem.MinMaxGradient
。