Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 NoobCarl · Mar 20, 2021 at 11:33 AM · materialgetcomponentrendererforeachgetcomponentinchildren

how do I change the properties of a material that are on the children of an object on script?

This is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TargetBehaviour : MonoBehaviour
 {
     [SerializeField] GameObject shatteredTarget;
     [SerializeField] float breakForce;
     [SerializeField] float durationDissolve = 1f;
     GameObject frac;
 
     public void DestroyTarget(Vector3 hitPos)
     {
         frac = Instantiate(shatteredTarget, transform.position, transform.rotation);
 
 
         foreach (Rigidbody rb in frac.GetComponentsInChildren<Rigidbody>())
         {
             Vector3 force = (rb.transform.position - transform.position).normalized * breakForce;
             rb.AddForceAtPosition(force, hitPos, ForceMode.Impulse);
             Debug.Log("Force applied");
         }
 
         StartCoroutine(CutOffEffect());
 
         Destroy(transform.parent.gameObject);
         Destroy(gameObject);
 
     }
 
     IEnumerator CutOffEffect()
     {
             foreach (Renderer fracRenderer in frac.GetComponentInChildren<Renderer>())
             {
                 float timePercent = 0f;
                 Debug.Log("Dissolve effect starting");
                 while (timePercent < 1)
                 {
                     timePercent += Time.deltaTime / durationDissolve;
                     fracRenderer.material.SetFloat("_Cutoff", Mathf.Lerp(0f, 1f, timePercent));
                     yield return null;
                 }
 
             }
         
     }
 
     
 }

On the IEnumerator CutOffEffect i'm trying to change the float of a property "_Cutoff" in every children's material that are on "frac".

 foreach (Renderer fracRenderer in frac.GetComponentInChildren<Renderer>())
             {
                 float timePercent = 0f;
                 Debug.Log("Dissolve effect starting");
                 while (timePercent < 1)
                 {
                     timePercent += Time.deltaTime / durationDissolve;
                     fracRenderer.material.SetFloat("_Cutoff", Mathf.Lerp(0f, 1f, timePercent));
                     yield return null;
                 }
 
             }

But on this foreach i get an error, i don't why.

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

Answer by rh_galaxy · Mar 21, 2021 at 05:10 PM

You cannot destroy the parent (and child objects) and expect the coroutine to continue to run on later frames. You must wait with destroy until CutOffEffect() has finished.

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

131 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 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

Changing two different objects renderer colour 1 Answer

Do something for each material in each renderer in children... 2 Answers

I'm trying to change a material, texture or color and it doesnt work 0 Answers

Change the materials of a list of game objects to one material, and then change it back to their old ones. 1 Answer

Getting a Material error from the default Sprite GameObject. (Unity 2017.3.0f3) 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