Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Darmouth · Sep 09, 2013 at 04:31 AM · c#particlesparticlesystemsimplelength

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?

Comment
Add comment · Show 16
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image perchik · Sep 09, 2013 at 04:32 AM 0
Share

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

avatar image Darmouth · Sep 10, 2013 at 02:16 AM 0
Share

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);
     }
 
 }
avatar image getyour411 · Sep 10, 2013 at 03:53 AM 0
Share

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...

avatar image zipa · Sep 10, 2013 at 04:26 AM 0
Share

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

avatar image Hoeloe · Sep 10, 2013 at 08:16 AM 1
Share

@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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darmouth · Sep 12, 2013 at 11:41 PM 0
Share

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.

avatar image Peter G · Sep 13, 2013 at 12:01 AM 0
Share

IEnumerator is the return type.

You need to refererence the System.Collections namespace.

 using System.Collections;
avatar image Darmouth · Sep 15, 2013 at 02:12 AM 0
Share

Your code works, with the exception of the particles not remaining stationary, which is another thing my code should do.

avatar image Peter G · Sep 15, 2013 at 02:33 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy particles based on bounds not lifetime 0 Answers

How would I attach a float to a particle? 1 Answer

Particles dissapearing when camera is close 1 Answer

How to get newest Particle in a ParticleSystem 1 Answer

Particle System wont play? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges