• 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 lampshade · May 28, 2010 at 09:51 PM · particlescolor

Changing color of Particles C#

Hi,

What I am trying to do is change the color of my particles which is a prefab attached to my game object. Everytime I press the 'h' key on the keyboard the particle system instantiates. I am trying to change the colors of each particle from white to red by iterating through the array.

I've tried to do this with the following code within an OnTriggerStay function:

//... private Particle[] PlayerParticles; public GameObject ParticlePrefab; // added to the player under its script private GameObject instantiated;

if (Input.GetKey("h") && instantiated == null)
{ instantiated = (GameObject)Instantiate(ParticlcePrefab, transform.position, Quaternion.identity);
PlayerParticles = particleEmitter.particles; // Error for (int i = 0; i < PlayerParticles.Length; i++) { PlayerParticles[i].color = Color.red; } particleEmitter.particles = PlayerParticles;

}

Getting the error:

MissingComponentException: There is no 'ParticleEmitter' attached to the "Player" game object, but a script is trying to access it. You probably need to add a ParticleEmitter to the game object "Player". Or your script needs to check if the component is attached before using it.

I know for a fact that the particle emitter is attached to the gameobject, player. Why would the particle emission happen upon pressing 'h' if it wasnt?

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
Best Answer

Answer by qJake · May 28, 2010 at 10:00 PM

When you instantiate something, you create a new game object. You cannot instantiate a component and place it on the current game object.

You need to either insert your particle emitter onto your game object in the editor, and then turn it on and off as needed at runtime, or create the particle emitter entirely with code (using AddComponent).

Comment
Add comment · Show 6 · 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 lampshade · May 28, 2010 at 10:19 PM 0
Share

Would I start by making a new script like, ParticleEmitter and addthat script to the player game object?

avatar image qJake · May 28, 2010 at 10:42 PM 0
Share

No, you can't make a script called "ParticleEmitter". That's a reserved word. I'm talking about adding the Emitter component to your current game object, using the Components menu at the top of Unity.

avatar image lampshade · May 28, 2010 at 11:06 PM 0
Share

Alright, I added the component (I guess I was just being stubborn, thinking I was right) and I am not getting the error, which is great. But I am still left with needing to change the particle color from white to red.

avatar image lampshade · May 28, 2010 at 11:16 PM 0
Share

Would I need to add the Particle Animator component ins$$anonymous$$d? I am not getting any color functionality with just the Ellipsoid Particle Emitter.

avatar image lampshade · May 28, 2010 at 11:17 PM 0
Share

That does not work b/c the code is wanting the Ellipsoid Particle Emitter, like I have.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Change color of Particles 1 Answer

Why Particle Animator Color Animation Do not Applied to Game Object? 1 Answer

Change color of individual particles 1 Answer

How to change the color of Particle System? 3 Answers

shader mobile / particles / additive with black & white texture turns out red 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges