MissingComponentException thrown with ParticleAttract script

Hello all,

Sorry if this is a basic question but I am very new to C# and Unity and can’t figure this out for the life of me.

Here is the code and problem…

Problem:
For some reason GetParticles always returns 0, even if used in LateUpdate. At the moment I’ve just set the length of the Particle array manually but I would like the script to be able to do it so that my colleagues don’t have to play around with the script each time.

Other problem is I need help making some lines work which keep throwing errors…

Code:

using UnityEngine;
using System.Collections;

public class ParticleAttract : MonoBehaviour 
{
    public int PullDistance;
    public ParticleSystem MagnetPoint;

    private Particle[] x = new Particle[50];
    private ParticleSystem y;
    private Transform t;
    private float d;

    //Use this for initialization
    void Start () 
    {
        t = transform;
        d = PullDistance*PullDistance;
   
        //x  = (ParticleSystem)(GameObject.Find(MagnetPoint).GetComponent(typeof(ParticleSystem)));
        //x = y.particles;


        string p = particleSystem.name;
        int o = particleSystem.maxParticles;

        Debug.Log(p);
        Debug.Log(o);
        Debug.Log(x);
    }

    void Update ()  
    {
        float n;
        for(int i = 0; i < x.Length; i++)
        {
            n = Vector3.SqrMagnitude(t.position - x[i].position);
            Debug.Log(d);
            if (n < d)
            {
                x[i].position = Vector3.Lerp(x[i].position, transform.position, Time.deltaTime / 2.0f);
                Debug.Log(x[i].position);
            }
        }
    }
}

I need to replace the commented out lines
//x = (ParticleEmitter)(GameObject.Find(MagnetPoint).GetComponent(typeof(ParticleEmitter)));
//x = y.particles;
With something that works.

Compiler errors are:
Line 20- The best overloaded method match for ‘…Find(string)’ has some invalid arguments.
Line 20- Argument #1 cannot convert ‘…’ expression to type ‘string’.
Line 21- ParticleSystem does not contain a definition for ‘particles’ etc etc

ok, let’s see:

  • If your array is private, you’ll need to set the length of it. (I’m not sure, so confirm it)
  • May I see the full erro message?

Do you know how to debug? You can see easely if your variable is assigned to the memory. Makes easy to fix the problem. :wink:

Probably not the correct code or correct error message anyway. There is no “particles” member of ParticleSystem. You can get the particles with the GetParticles() method. http://docs.unity3d.com/Documentation/ScriptReference/ParticleSystem.GetParticles.html

There is, however, a particles member on ParticleEmitter which is the legacy particle system. http://docs.unity3d.com/Documentation/ScriptReference/ParticleEmitter-particles.html

Thanks guys I’m running over the scripting tutorials at the moment. And I can write to Debug now so that will give me some much needed feedback. I am working on a project that has been in development for over a year - when I create a particle system, how can I tell if it’s type is a ParticleSystem or ParticleEmitter? Using Unity 4.3. Cheers!

By looking at the name of the component in the Inspector…

Also - you’re drag and drop would fail if you tried to drag the wrong type of object onto the field.

I’ve updated the code in the main post. So now that no compile errors are being thrown, the problem is that GetParticles never returns the amount of particles that will be spawned - I can use maxParticles since it is a burst particle emitter of exactly 50 particles however I figure I will need to learn how to use GetParticles properly at some point in time! Thanks again

I’m requesting this be deleted as the title is now misleading and I’m going to make a more summarised post now of what I’m trying to do and the updated code! Thanks to Kelso and PJRM for your help :slight_smile:

It’s generally helpful to leave threads as-is in case folks come across it while searching for answers to similar issues.