gpt4 book ai didi

c# - 如何修复 : NullReferenceException: Do not create your own module instances, 从 ParticleSystem 实例获取它们

转载 作者:行者123 更新时间:2023-12-05 03:01:45 30 4
gpt4 key购买 nike

我正在尝试制作一个脚本,以便在按下按钮时粒子系统更改为某种颜色,除了更改粒子颜色外,一切正常,当我尝试它时出现此错误:

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 类型。

关于c# - 如何修复 : NullReferenceException: Do not create your own module instances, 从 ParticleSystem 实例获取它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55562829/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com