【发布时间】:2020-02-04 14:34:56
【问题描述】:
我创建了一个粒子系统,并且在我的游戏中的某个时间点我想增加最大粒子数。
因此,我创建了一个游戏对象,并在其上附加了一个盒子 2d 对撞机,当它与玩家接触时会触发。在该脚本中,我创建了一个公共 GameObject 叶粒子系统,并将粒子系统拖入其中。
但是,在我的代码中,我无法执行类似的操作
leafParticleSystem.maxParticles=500;
或
leafParticleSystem.emission.rate=5.0f
我的代码也在说
"MissingComponentException: There is no 'ParticleSystem' attached to the "Herld" game object, but a script is trying to access it.
You probably need to add a ParticleSystem to the game object "Herld". Or your script needs to check if the component is attached before using it.untime/Export/Coroutines.cs:17)"
然而,这个“Herld”游戏对象不是粒子系统(我也不想给它添加一个)但是,它确实附有我将粒子系统(叶子粒子系统)拖到的脚本。我只是想从“Herld”对象中的代码修改叶粒子系统。
如果我能得到任何帮助将不胜感激
附上脚本
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class WindEffect : MonoBehaviour {
// Rigidbody2D _rigidbody;
// Use this for initialization
//Storm Warning
public GameObject AOE;
public GameObject UIStormInterface;
public GameObject UIWindZone;
public GameObject LeafStormParticleSystem;
public float ActivateFor=5.0f;
public float stormLength=5.0f;
void Start () {
}
// Update is called once per frame
void Update () {
}
void Awake () {
// _rigidbody = GetComponent<Rigidbody2D> ();
}
void OnTriggerEnter2D(Collider2D col)
{
// Debug.Log("Object touched trigger");
// Debug.Log (col.transform.name);
if (col.gameObject.name == "Player") {
Debug.Log ("Collided with Storm Herald");
StartCoroutine (TemporarilyActivateStormWarning (ActivateFor));
}
}
private IEnumerator TemporarilyActivateStormWarning(float ActivateFor) {
//Show Storm Warning
this.gameObject.GetComponent<BoxCollider2D>().enabled=(false);
UIStormInterface.GetComponent<Text>().text="A Violent Storm is Approaching";
UIStormInterface.SetActive(true);
UIWindZone.SetActive (true);
//Wait for 5 seconds then activate storm for 5 seconds
yield return new WaitForSeconds(ActivateFor);
UIStormInterface.SetActive(false);
AOE.SetActive (true);
// ParticleSystem LeafStormParticleSystem = GetComponent<ParticleSystem>();
// LeafStormParticleSystem.emission.rate = 5.0f;
LeafStormParticleSystem.maxParticles = 500;
yield return new WaitForSeconds(stormLength);
//Turn off Storm
AOE.SetActive (false);
//Tell user storm is stopped for 2 seconds.
UIStormInterface.GetComponent<Text>().text="Storm has Passed";
UIStormInterface.SetActive(true);
yield return new WaitForSeconds(3);
UIStormInterface.SetActive(false);
UIWindZone.SetActive (false);
}
void OnTriggerStay2D(Collider2D col)
{
if (col.gameObject.name == "WindObject") {
Debug.Log ("Collided");
// Debug.Log (col.transform.name);
// Wind();
//StartCoroutine(WindStart());
}
}
/*
IEnumerator WindStart(){
Debug.LogError ("Slow Time.");
//SlowTheTime();
// yield return new WaitForSeconds(3.5f);
// Debug.LogError ("Slow Time.");
// Wind ();
}*/
void OnCollisionEnter2D(Collision2D col)
{
// Debug.Log (col.transform.name);
}
void OnTriggerStay(Collider col)
{
Debug.Log("Object is in trigger");
}
void OnTriggerExit(Collider other)
{
Debug.Log("Object left the trigger");
}
/*
void Wind(){
_rigidbody = GetComponent<Rigidbody2D> ();
Debug.Log ("test got here");
Debug.Log (this.gameObject.name);
this._rigidbody.AddForce (-Vector2.left * 100 );
}*/
}
【问题讨论】:
-
你能包含附加到“Herld”游戏对象的脚本吗?
leafParticleSystem是对它的引用吗? -
包含脚本,是的,我将它拖放到公共游戏对象中,就像我用来访问分数和画布等的所有其他公共变量一样
-
谢谢。所以让我看看我是否理解这种情况...
leafParticleSystem是一个公共 GameObject 变量,您将 GameObject“Herld”(附加了WindEffect组件)拖到该变量上?而你要访问的粒子系统存储在WindEffect脚本的公共变量LeafStormParticleSystem中? -
不,leafParticleSystem 是公共 GameObject 变量,我将粒子对象从 heirachy 拖到该变量上。是的,GameObject Herald 确实附加了 windEffect 组件,是的,我要编辑的粒子是 LeafStormParticleSystem
-
嗯...嗯,
leafParticleSystem是带有粒子系统的游戏对象,还是LeafStormParticleSystem?根据您的上一条评论,我无法想象这种关系。