- Home /
Problem using point.Length in a particle system c#
All I want to do is generate a length of discrete points along a line using a particle system. The rest of my code is fine, with the exception of this part, which is supposed to actually generate the particles. code:
void Update() {
particleSystem.SetParticles(points, points.Length);
}
When I hover over the ".Length" part, it says it's an unresolved member. Unity also gives me 203 of this error message: "Object reference not set to an instance of an object," and each sends me to the line of code in Update. What am I doing wrong? How do I fix this?
the rest of your code may be fine, but we need to see it to solve your problem. I'm about 95% confident that the problem lies when you create and initialize the points object
Okay, the code in full is:
using UnityEngine;
public class Grapher1 : $$anonymous$$onoBehaviour {
public int resolution = 10;
private ParticleSystem.Particle[] points;
void Start () {
if (resolution < 10 || resolution > 100) {
Debug.LogWarning("Grapher resolution out of bounds, resetting to $$anonymous$$imum.", this);
resolution = 10;
}
points = new ParticleSystem.Particle[resolution];
float increment = 1f / (resolution - 1);
for (int i = 0; i < resolution; i++) {
float x = i * increment;
points[i].position = new Vector3(x, 0f, 0f);
points[i].color = new Color(x, 0f, 0f);
points[i].size = 0.1f;
}
}
void Update () {
particleSystem.SetParticles(points, points.Length);
}
}
At the top are you missing "using System.Collections;"? I don't know C# syntax that well but since a common Array property (Length) is giving you fits, makes me wonder...
I tried to use your script and it works. $$anonymous$$aybe you just forgot to add a ParticleSystem component to your GameObject in the Editor or you can do on the start of this script: gameObject.AddComponent();
@ArkaneX No it shouldn't. ParticleSystem is a class, while particleSystem is an implicit GetComponent
call, returning the attached ParticleSystem. SetParticles
is not a static method, so you can't call it from the class, only from an instance.
Answer by Darmouth · Sep 16, 2013 at 04:12 AM
Oh my goodness. Apparently my code was written as:
void start() { codecodecode...}
and not
void Start() { codecodecode...}
so it didn't recognize the start method, and didn't run it upon startup.
My code works fine now. I have no idea how I missed that.
Answer by Peter G · Sep 11, 2013 at 04:52 PM
From my experience, you actually have to get the particles before you can set them. Not every frame, but at least once you need to get some existing particles before you change them and reassign them.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public int resolution = 10;
private ParticleSystem.Particle[] points;
void Start () {
if (resolution < 10 || resolution > 100) {
Debug.LogWarning("Grapher resolution out of bounds, resetting to minimum.", this);
resolution = 10;
}
points = new ParticleSystem.Particle[resolution];
particleSystem.Emit(resolution);
StartCoroutine( SetupParticles() );
}
IEnumerator SetupParticles () {
yield return null;
particleSystem.GetParticles( points );
float increment = 1f / (resolution - 1);
for (int i = 0; i < resolution; i++) {
float x = i * increment;
points[i].position = new Vector3(x, 0f, 0f);
points[i].color = new Color(x, 0f, 0f);
points[i].size = 0.1f;
}
particleSystem.SetParticles(points, resolution);
}
}
I'd try something like that. I emitted resolution
particles, got them, then modified them before reassigning them to the original. You can't access particles immediately after they are created. That's why I yielded a frame before accessing them.
IEnumerator looks like an undeclared variable to monodevelop, so it says there are compiling errors when I try to use it, or just your code.
IEnumerator
is the return type.
You need to refererence the System.Collections
namespace.
using System.Collections;
Your code works, with the exception of the particles not remaining stationary, which is another thing my code should do.
You can fix that in the editor. You just need to set the initial speed to 0 in the particle editor.
Or you can just set the initial velocity to 0 via code. It's a property of particleSystem.