Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KingSpud91 · Aug 20, 2017 at 02:35 PM · materialgamecolor change

Set enemy death particle to the colour of the enemy itself

Hey Guys, Im currently stuck on a small issue where i have a bunch of enemy objects that rush towards the player character and try to attack the player. The player is able to shoot and kill the attacking enemies.

Currently ive got my enemies spawning with random colours, though the prefab has a preset material set to it. Code for enemy spawning is below:

 //Spawn the enemy at the center of the plane
 Enemy spawnedEnemy = Instantiate (enemy, spawnTile.position + Vector3.up, Quaternion.identity) as Enemy;
 //Spawn and enemy with a random colour
 spawnedEnemy.GetComponent<MeshRenderer>().material.color = new Color(Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));

So for example if i start the game one of the enemies could be blue and the other could be green.

Ive currently got a particle system in place that activates on the death of the enemy. So once the enemy dies it blows up into small cubes.

I was just wondering how i can change the colour of the explosion for each enemy based on the random colour that theyre instantiated with.

For example if i killed a blue enemy then the partical system would emit blue cubes, if the enemy was green then the particle system would emit green cubes.

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

Answer by hfiani · Aug 20, 2017 at 03:22 PM

try to give the material in renderer in particle system the same material as the original one.

if not try this:

 spawnedEnemy.GetComponent<ParticleSystemRenderer> ().material.color = spawnedEnemy.GetComponent<SpriteRenderer> ().material.color;
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 KingSpud91 · Aug 20, 2017 at 03:28 PM 0
Share

Sorry I dont think you understood my question. i WANT all my enemies to have random colours and they currently are spawning with random colours.

I basically want the partical explosion matterial to change to the current colour of the enemy thats been killed.

avatar image hfiani · Aug 20, 2017 at 04:22 PM 0
Share

my answer still applies. i understood 100% your question. this:

 spawnedEnemy.GetComponent<ParticleSystemRenderer> ().material.color

means i assumed that the particle system is attached to the enemy himself, is this is not the case, replace spawnedEnemy qith the particle object, like this:

 particleObject.GetComponent<ParticleSystemRenderer> ().material.color = spawnedEnemy.GetComponent<SpriteRenderer> ().material.color;

avatar image KingSpud91 hfiani · Aug 20, 2017 at 04:44 PM 0
Share

that doesnt really seem to work. This is the code i have so far:

 if (damage >= health) {
             Destroy(Instantiate(deathEffect.gameObject, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject, deathEffect.startLifetime);
             deathEffect.GetComponent<ParticleSystemRenderer> ().material = transform.GetComponent<$$anonymous$$eshRenderer> ().material;
         }

which is in the enemy script as the Particle System Prefab is attached as a component to the enemy prefab

avatar image hfiani · Aug 20, 2017 at 04:49 PM 0
Share

your code is not understandable actually, you are instantiating an object and destroying it in one instruction. AFTER that you are changing the color of the ORIGINAL particle object, and not the one you created.

I don't know if this will work for you but try changing the color (not the whole material in this case) before the destroy(instantiate(...))

 if (damage >= health) {
     deathEffect.GetComponent<ParticleSystemRenderer> ().material.color = transform.GetComponent<$$anonymous$$eshRenderer> ().material.color;
     Destroy(Instantiate(deathEffect.gameObject, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject, deathEffect.startLifetime);
 } 

avatar image hfiani · Aug 20, 2017 at 04:51 PM 0
Share

But i will do this:

 if (damage >= health) {
     GameObject deathEffectTemp = Instantiate(deathEffect.gameObject, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject;
     deathEffectTemp.GetComponent<ParticleSystemRenderer> ().material.color = transform.GetComponent<$$anonymous$$eshRenderer> ().material.color;
     Destroy(deathEffectTemp, deathEffect.startLifetime);
 }








avatar image KingSpud91 hfiani · Aug 20, 2017 at 10:30 PM 0
Share

The second code works! Thank you very much!

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

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

Related Questions

Object material showing in scene but not game view 2 Answers

Change color of all instance of a material 1 Answer

Changing material color from dictionary (C#) 0 Answers

Unity 2019.3.2f1 Unity Skybox Color Tint No Effect 0 Answers

Color problem with Unity 5 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