I came across this code today: Code (CSharp): // Get the latest webcam shot from outside "Friday's" in Times Square using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg"; IEnumerator Start() { WWW www = new WWW(url); yield return www; Renderer renderer = GetComponent<Renderer>(); renderer.material.mainTexture = www.texture; } } It is from here: https://docs.unity3d.com/ScriptReference/WWW.html Can someone help me understand it? It seems to run automatically when attached to an object? I was aware of the “void Start(void)” which gets called automatically but was surprised to see this automatically called. Thanks.
Take a look at this link: http://answers.unity3d.com/questions/754968/can-monobehaviorstart-return-an-ienumerator-hint-y.html
Here is some comments in that code about what it is doing. Code (csharp): using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { // the url to load public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg"; // IEnumerators are things that can run outside of the // normal Update/FixedUpdate process // Start can be an IEnumerator, so it can run things and wait to load IEnumerator Start() { // create the WWW object and have it load the url from above WWW www = new WWW(url); // this waits until that has been loaded. yield return www; // find the renderer of this object Renderer renderer = GetComponent<Renderer>(); // apply the texture to the mainTexture portion of that material renderer.material.mainTexture = www.texture; } }