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
1
Question by Ezequiel 1 · Dec 23, 2010 at 12:07 PM · particleanimator

Why doesn't my code seem to change particle color?

Hi, im trying to change the color of the particles but it doesnt seems to work... Please, can you help me and tell me what im doing wrong?? . This is the script that i attached to my particle system:

ParticleAnimator particleAnimator = (ParticleAnimator)GetComponent("PS");
Color[] modifiedColors = particleAnimator.colorAnimation;
modifiedColors[0] = Color.red;
modifiedColors[1] = Color.blue;
modifiedColors[2] = Color.red;
modifiedColors[3] = Color.blue;
modifiedColors[4] = Color.red;
particleAnimator.colorAnimation = modifiedColors;

PS is de name of the particle system. Thanks!!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Statement · Dec 23, 2010 at 12:37 PM

// The faulty code:
ParticleAnimator particleAnimator = (ParticleAnimator)GetComponent("PS");

In your code, you're trying to get a component using Component.GetComponent that is of the type "PS". This is an error. You must supply the component type, not the gameobject name.

This will result in either:

  • Null reference exception when you try to use it (since there was no component of type PS found).
  • Invalid cast exception (if PS is a type that exist and is placed on the game object).

Try this code instead (I use the generic overload since it seems we're dealing with C#):

var particleAnimator = GetComponent<ParticleAnimator>();

// Same as: // var particleAnimator = (ParticleAnimator)GetComponent("ParticleAnimator"); // But alot cleaner imo.

If your particle animator is not on the same object as your script (the "PS" object, I reckon), then you can find it using GameObject.Find and access it using GameObject.GetComponent like such:

var ps = GameObject.Find("PS");
var particleAnimator = ps.GetComponent<ParticleAnimator>();

  • Note I used var keyword to shorten my post and make it more readable. This is not required.
Comment
Add comment · Show 2 · 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 Ezequiel 1 · Dec 23, 2010 at 01:09 PM 0
Share

Thanks!!!!!!!!!

avatar image Statement · Dec 23, 2010 at 07:38 PM 0
Share

Consider accepting the answer if you deem it correct. You can always pick another answer to be accepted later should a better answer arrive.

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

No one has followed this question yet.

Related Questions

How to make Gas? 2 Answers

Invalid Cast Exception Error (on instantiation) 1 Answer

Where does ParticleAnimator update in the Event Execution Order? 0 Answers

Shoot particles like motorcycle roost... 0 Answers

scripting a trigger to emit a flame on my character then.. 5 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