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 hardmode2236 · Dec 08, 2014 at 06:13 AM · arrayparticlesnullreferenceexception

particle array null refrence

I'm just messing around trying to add some external effects to my particles(particle attraction in this case). but it seems my particle array isn't being filled, or something is going wrong when i try to fill it with my particleSystem's getParticles() function. i must be creating the array wrong or trying to fill it incorrectly. can anyone see my problem?

 #pragma strict
 var target : Transform;
 var power = 5.0;
 var PA: ParticleSystem.Particle[];
 var heading:Vector3;
 function Start () 
 {
 
 }
 
 function Update () 
 {
     particleSystem.GetParticles(PA);
      for(var p in PA)
      {
          heading = target.position - p.position;
          p.velocity += heading.normalized * power;
      }
      particleSystem.SetParticles(PA, PA.Length);
 }
 

error is at the 'for' line:

NullReferenceException: Object reference not set to an instance of an object

i also tried this for loop, but got the same error:

      for(var i = 0; i < PA.Length; i++)
      {
          heading = target.position - PA[i].position;
          PA[i].velocity += heading.normalized * power;
      }


Comment
Add comment · Show 3
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 zharik86 · Dec 08, 2014 at 08:32 AM 0
Share

Are you attach this script at GameObject with your ParticleSystem?

avatar image hardmode2236 · Dec 08, 2014 at 08:38 AM 0
Share

Yeah, it's on the object with the particle system.

avatar image EvilTak · Dec 08, 2014 at 09:48 AM 0
Share

Try caching your particle system and adding it manually from the editor.

1 Reply

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

Answer by zharik86 · Dec 08, 2014 at 10:17 AM

I think, you dont't using prewarm options for your particle. Than, in 1st frame count of your particle, probably, is 0. Than for 1st frame array PA is null. Than:

  #pragma strict
  var target : Transform;
  var power = 5.0;
  //Initialization your array
  var PA: ParticleSystem.Particle[] = new ParticleSystem.Particle[1000];
  var heading:Vector3;
  function Start () {
 
  }

  function Update ()  {
   var myLen: int = particleSystem.GetParticles(PA);
   if (PA != null) {
    for(var i: int = 0; i < myLen; i++) {
     heading = target.position - PA[i].position;
     PA[i].velocity += heading.normalized * power;
    }
    particleSystem.SetParticles(PA, myLen);
   }
  }

Of course, you use GetParticle in "if": return number of particle. I hope that it will help you.

Comment
Add comment · Show 3 · 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 hardmode2236 · Dec 08, 2014 at 06:44 PM 0
Share

oh yeah thanks, i forgot to handle the exceptions. using this code i got rid of the exception:

 particleSystem.GetParticles(PA);
     if(PA != null)
     {
         for(var p in PA)
         {
             heading = target.position - p.position;
             p.velocity += heading.normalized * power;
             Debug.Log("Working");
         }
         particleSystem.SetParticles(PA, PA.Length);
     }

But still doesn't run the 'debug' line. which means my array still isnt filling with particles.

avatar image zharik86 · Dec 08, 2014 at 07:47 PM 0
Share

Ok, I modify my answer. I checking it. All work.

avatar image hardmode2236 · Dec 09, 2014 at 07:16 AM 0
Share

sorry i was away from my computer today, but thank you this works perfectly now. :)

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException weird error 1 Answer

Array values not being initialised unless the object with the (Player) script is selected in the inspector whilst the game is on 1 Answer

Writing to txt file using textfield problem 2 Answers

Accessing individual particles in array? 1 Answer

Error putting an object into an Array 0 Answers


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