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 /
  • Help Room /
avatar image
0
Question by vauthier · Jan 20, 2017 at 10:17 PM · coroutineimagevideo

How i can fix this code ?

Hello, I want to play multiple pictures to make a video effect. I try videotexture but its very unstable. I try with coroutine but its looping all the time the condition its true but I just playing the pictures only once. DO you have an idea ? Thanks!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Salma : MonoBehaviour
 {
 
     
 
     public Texture Avertissement1;
     public Texture Avertissement2;
     public Texture Avertissement3;
     public Texture Avertissement4;
     public Texture Avertissement5;
     public Texture Avertissement6;
     public Texture Avertissement7;
     public Texture Avertissement8;
     public Texture Avertissement9;
     public Texture Avertissement10;
     public Texture Avertissement11;
     public Texture Avertissement12;
     public Texture Avertissement13;
     
 
     public GameObject object1;
     public GameObject object2; 
 
 
         void Update ()
         {
                 float distance = Vector3.Distance (object1.transform.position, object2.transform.position);
                 print (distance);
 
 
                 if (distance >= 10 && distance < 20 )
 
                 { 
 
                         StartCoroutine (Disparition());
 
 
                 } 
         
         }
         
 
         IEnumerator Disparition ()
         {
 
                 yield return null;
                 GetComponent<RawImage> ().texture = Avertissement1 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement2 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement3 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement4 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement5 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement6 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement7 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement8 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement9 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement10 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement11 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement12 as Texture;
                 yield return new WaitForSeconds (0.3f);
                 GetComponent<RawImage> ().texture = Avertissement13 as Texture;
 
 
         }
         }
         

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 HenryStrattonFW · Jan 20, 2017 at 10:58 PM

I'd suggest using an array instead of multiple variables (iterations if your friend, be nice to it, buy it chocolate, keep its trust). Also since your values are already of type Texture you do not need to use the "as" keyword.

EDIT: Updating the example code to provide more detail closer to original, if you StartCoroutine all the time you will create multiple routines causing the image to jump around a bit. So only play the routine if it is not already running.

ANOTHER EDIT: updated code to cache the RawImage component only once, since there is no sense in getting the component over and over again.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Test : MonoBehaviour
 {
     [SerializeField]
     private Texture[] m_Textures;
     [SerializeField]
     private GameObject m_Object1;
     [SerializeField]
     private GameObject m_Object2;
 
     private Coroutine m_Sequence;
     private RawImage m_RawImage;
 
     void Update()
     {
         float distance = Vector3.Distance(m_Object1.transform.position, m_Object2.transform.position);
         print(distance);
 
         if (distance >= 10 && distance < 20)
         {
             if (!m_Sequence == null)
             {
                 m_Sequence = StartCoroutine(Disparition(m_Textures));
             }
         }
         else
         {
             StopCoroutine(m_Sequence);
         }
     }
 
     private IEnumerator Disparition(Texture[] lImages)
     {
         if (m_RawImage == null)
         {
             m_RawImage = GetComponent<RawImage>();
         }
         int lIndex = 0;
         while (lIndex < lImages.Length)
         {
             // Display image.
             m_RawImage.texture = lImages[lIndex] as Texture;
             yield return new WaitForSeconds(0.3f);
             lIndex++;
         }
         m_Sequence = null;
     }
 }
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

92 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

Related Questions

MovieTexture.duration returning consistently incorrect duration 1 Answer

Playing a Video on raw image 0 Answers

Coroutine Not Starting - Crashes Instead? 1 Answer

transform.position not applying to my camera. 1 Answer

Change color for one second then revert to normal. 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