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
2
Question by lampshade · May 29, 2010 at 06:53 PM · particlescolor

Change color of Particles

Hi,

What I have is an Ellipsoid Particle Emitter, Particle Animator, Particle Renderer, and a World Particle Collider (in that order) attached to a prefab that I have named "ParticlePrefab". The ParticlePrefab is attached to my Player GameObject.

Everytime I press 'h' on the keyboard, I see particles emit, which is what I want. Now I need to be able to change the color of the particles, through a script. Here is what I have so far:

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {
// Allow our PlayerClass awareness of the Particle public GameObject ParticlePrefab; private GameObject instantiated;

private Vector3 lastPosition;
// Values are set in Unity3D Interface public float MoveSpeed; public float RotateSpeed;

// Update is called once per frame void Update () {

 Move_Player();

}

void OnTriggerStay(Collider other) {

 if (Input.GetKeyDown("h") && instantiated == null)  
 {

     instantiated = (GameObject) Instantiate(ParticlePrefab, transform.position, Quaternion.identity);

     //Particle [] p = p.ColorAnimator = Color.red;

     Particle [] PlayerParticles = particleEmitter.particles;
     for (int i = 0; i < PlayerParticles.Length; i++)
     {
         PlayerParticles[i].color = Color.red;
     }
     particleEmitter.particles = PlayerParticles;

     Color[] MyColor = particleAnimator.colorAnimation;
     MyColor[4] = Color.red;
     particleAnimator.colorAnimation = MyColor;

 }

} void Move_Player() { float MoveForward = Input.GetAxis("Vertical") MoveSpeed Time.deltaTime; float MoveRotate = Input.GetAxis("Horizontal") RotateSpeed Time.deltaTime;

 // Move the player
 transform.Translate(Vector3.forward * MoveForward);
 transform.Rotate(Vector3.up * MoveRotate);

 if (Vector3.Distance(lastPosition, transform.position) > 0.01f)
 {
     lastPosition = transform.position;

     if (instantiated != null)
     {
         instantiated.particleEmitter.emit = false;
         Destroy(instantiated, 0.90f);
     }
 }

}

}

If this cannot be done through a script, could someone show me how to use AddComponent so that I can add a particle emitter, of which i will color, the player component and get it to work in the same way?

Comment
Add comment · Show 1
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 29, 2010 at 07:04 PM 0
Share

Alright so I made a new particle emitter for red particles and just added to a prefab, deleted the system, and added the prefab to my player Game Object and it worked. But I would prefer to change it through code how can I do that?

2 Replies

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

Answer by Peter G · May 29, 2010 at 08:06 PM

Here, this will change your colors programatically. I didn't touch much of it so a large part is hard coded like you had it.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class NewBehaviourScript : MonoBehaviour {
// Allow our PlayerClass awareness of the Particle public ParticleEmitter Particles;

// Values are set in Unity3D Interface public float MoveSpeed; public float RotateSpeed;

private ParticleEmitter spawnedParticles; private ParticleAnimator particleAnimator;

private Vector3 lastPosition;

// Update is called once per frame void Update () {

 Move_Player();

}

void OnTriggerStay(Collider other) {

 if (Input.GetKeyDown("h") && spawnedParticles == null)  
 {

     spawnedParticles = Instantiate(Particles, transform.position, Quaternion.identity) as ParticleEmitter;

     particleAnimator = spawnedParticles.GetComponent<ParticleAnimator>();
     Color[] m_Color = particleAnimator.colorAnimation;
     m_Color[4] = Color.red;
     particleAnimator.colorAnimation = m_Color;


 }

} void Move_Player() { float MoveForward = Input.GetAxis("Vertical") MoveSpeed Time.deltaTime; float MoveRotate = Input.GetAxis("Horizontal") RotateSpeed Time.deltaTime;

 // Move the player
 transform.Translate(Vector3.forward * MoveForward);
 transform.Rotate(Vector3.up * MoveRotate);

 if (Vector3.Distance(lastPosition, transform.position) > 0.01f)
 {
     lastPosition = transform.position;

     if (spawnedParticles != null)
     {
         spawnedParticles.emit = false;
         Destroy(spawnedParticles.gameObject, 0.90f);
     }
 }

} }

But unless there is something wrong with the way you have it, using a prefab is likely faster and easier. The old adage "Why fix something that's not broken." seems fitting here.

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 lampshade · May 29, 2010 at 08:23 PM 0
Share

The definite solution. Thanks =))

avatar image Griever_GF · Jun 30, 2015 at 10:06 AM 0
Share

Any example of simular code for new (shuriken) particle system?

avatar image
-1

Answer by Joe 17 · Feb 27, 2011 at 08:47 AM

I'm having the same problem...... I have particle emitters that i want to change the colour of procedurally (as in to flow through from say green to red over time when i prompt it to) how would I go about doing this?

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

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

Trying to change the alpha on a particle system. 1 Answer

Using Colour over Lifetime with particles that have a Texture Sheet Animation 0 Answers

Can't make black/grey smoke? - Particle Systems 0 Answers

Accessing material of particle renderer?? 1 Answer

Is there a way to control the color of vfx graph particles in 3 directions from property? 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