我有一个脚本的问题。我正在尝试在Unity场景中为球体随机创建星空。但我对Unity和C#都很陌生,所以有点困惑。
星星位置固定,因此它们不应移动,并且在Start()中创建,然后在Update()中绘制。
问题是我收到了这个错误:
MissingComponentException: There is no 'ParticleSystem' attached to the "StarField" game object, but a script is trying to access it.
You probably need to add a ParticleSystem to the game object "StarField". Or your script needs to check if the component is attached before using it.
Stars.Update () (at Assets/Stars.cs:31)
如果我手动添加粒子系统组件,它会导致大量闪烁的橙色斑点,这不是我想要的,因此我想以某种方式在脚本中添加该组件。
这是我的脚本附加到一个空游戏对象上:
using UnityEngine;
using System.Collections;
public class Stars : MonoBehaviour {
public int maxStars = 1000;
public int universeSize = 10;
private ParticleSystem.Particle[] points;
private void Create(){
points = new ParticleSystem.Particle[maxStars];
for (int i = 0; i < maxStars; i++) {
points[i].position = Random.insideUnitSphere * universeSize;
points[i].startSize = Random.Range (0.05f, 0.05f);
points[i].startColor = new Color (1, 1, 1, 1);
}
}
void Start() {
Create ();
}
// Update is called once per frame
void Update () {
if (points != null) {
GetComponent<ParticleSystem>().SetParticles (points, points.Length);
}
}
}
我该如何设置静态星空呢?手动添加粒子系统组件会给我带来这些令人讨厌的橙色粒子,我想通过脚本纯粹地实现它。