Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 martipello · Feb 06, 2016 at 08:53 AM · renderingmaterialcoroutine

change material on background

im trying to change the material of a plane at runtime based on a co routine it should be one material for 3 seconds and then the other for 3 seconds im trying to do something like this

 public Material mat1;
 public Material mat2;
 bool material = false;

 void Start () {
     current = this;
     GetComponent<Renderer>().material = mat1;
 }

 IEnumerator MyCoroutine()
 {
     bubbles();
     yield return new WaitForSeconds(3.0f);
     clouds();
     yield return new WaitForSeconds(3.0f);
 }
   

 // Update is called once per frame
 void Update () {
     StartCoroutine(MyCoroutine());
 }
 void bubbles()
 {
     if (material == false)
     {
         GetComponent<Renderer>().material = mat2;
         go();
         material = true;
     }
 }
 void clouds()
 {
     if (material == true)
     {
         GetComponent<Renderer>().material = mat1;
         go();
         material = false;
     }
 }
 public void go()
 {
     pos += speed;
     if (pos > 1.0f)
         pos -= 1.0f;
     GetComponent<Renderer>().material.mainTextureOffset = new Vector2(pos, 0);
 }

im kinda new to unity and coding really but in my eyes this should work its meant to have a parallax effect which does work, and it does change material but the material kinda flickers and stutters back and forth is there something im not accounting for? also while im asking is there any way to parallax a sprite? not in the same way obviously as im using texture offset right now thanks for any and all suggestions

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

4 Replies

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

Answer by Halfbiscuit · Feb 06, 2016 at 11:36 AM

Your issue is you are starting a new coroutine every frame in the Update() function. In this case you only want one coroutine.

Try moving the StartCoroutine(MyCoroutine()); call to your Start() function then either put a while loop in your coroutine or have it call itself at the end.

Comment
Add comment · Show 3 · 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 martipello · Feb 06, 2016 at 12:34 PM 0
Share

Thank you :-)

avatar image martipello · Feb 06, 2016 at 04:33 PM 0
Share

okay i only just got to my computer to check this out @buFFalo94 i think your suggestion is for a gameObject array but again im new here so please correct me if im wrong, however @Halfbiscuit (cool name btw) your solution is PERFECT! so this is what i now have

 `void Start () {
     current = this;
     GetComponent<Renderer>().material = mat1;
     StartCoroutine($$anonymous$$yCoroutine());
 }

 IEnumerator $$anonymous$$yCoroutine()
 {
     bubbles();
     yield return new WaitForSeconds(3.0f);
     clouds();
     yield return new WaitForSeconds(3.0f);
     StartCoroutine($$anonymous$$yCoroutine());
 }`
avatar image martipello · Feb 06, 2016 at 04:43 PM 0
Share

in fact looking at it now it is definitely changing the material smoothly and consistently howeverthe parallax effect is now not working

avatar image
0

Answer by buFFalo94 · Feb 06, 2016 at 02:22 PM

if GetComponent().material = mat1 does not work try GetComponent().sharedmaterial= mat1;

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

Answer by buFFalo94 · Feb 06, 2016 at 02:22 PM

try GetComponent().sharedMaterial= mat2;

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

Answer by buFFalo94 · Feb 06, 2016 at 02:22 PM

try renderer.sharedMaterial instead of renderer.material hope it help

Comment
Add comment · Show 1 · 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 martipello · Feb 06, 2016 at 02:45 PM 0
Share

I've just steppes away from it I'll be back on soon and update with what I did thank you for your replies

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

42 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

Related Questions

See through issue in iOs 0 Answers

Removing blue line around object's edges 1 Answer

Image-Based Lighting refreshing 0 Answers

rendering mode "Fade" results in culling disorder? 0 Answers

How do I use a different UV channel for the secondary map of the standard shader in URP? 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