Particles to create star field in unity

WDUK picture WDUK · Mar 10, 2016 · Viewed 8.4k times · Source

I have a issue with a script. I am trying to create a star field randomly in a sphere for my unity scene. But I am new to unity and c# so I am a bit confused.

The stars have a fixed place so they should not move and so are created in Start(); and then are drawn in Update();

The problem is I get this error:

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)

If i add a particle system component manually it causes a load of big flashing orange spots, which i don't want, so i want to add the component in the script some how.

This is my script attached to an empty game object:

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);

        }
    }
}

How can i set it to get a static star field, because adding a particle system component manually gives me these annoying orange particles and am wanting to do it purely via scripts.

Answer

James Bateson picture James Bateson · Mar 10, 2016

It will be easier for you if you add the particle system manually and change the settings so that you don't see any funny shapes at runtime or in the Editor.

As a side note, you don't need to set the particles every frame in Update. Even if you did, calling GetComponent is expensive, so you should save the ParticleSystem as a field of the class in the Start() method.

Here is some modified code that worked for me:

using UnityEngine;

public class Starfield : MonoBehaviour 
{

    public int maxStars = 1000;
    public int universeSize = 10;

    private ParticleSystem.Particle[] points;

    private ParticleSystem particleSystem;

    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);
        }

        particleSystem = gameObject.GetComponent<ParticleSystem>();

        particleSystem.SetParticles(points, points.Length);
    }

    void Start()
    {
        Create();
    }

    void Update()
    {
        //You can access the particleSystem here if you wish
    }
}

Here is a screenshot of the starfield with the settings used in the particle system. Note that I switched off looping and play on awake.

Settings