-2
public class Scroll : MonoBehaviour {

    public float speed = 0.5f;

    void Update()
    {
        Vector2 offset = new Vector2(0, Time.time * speed);

       renderer.material.mainTextureOffset = offset;

    }

i didn't understand where the problem is, but it is supposed to be in the renderer.material part.

I have put this code in a Quad to try and make it my background.

edit: error messages : - Assets/Scroll.cs(12,8): error CS0619: UnityEngine.Component.renderer' is obsolete:Property renderer has been deprecated. Use GetComponent() instead. (UnityUpgradable)' - Assets/Scroll.cs(12,17): error CS1061: Type UnityEngine.Component' does not contain a definition formaterial' and no extension method material' of typeUnityEngine.Component' could be found (are you missing a using directive or an assembly reference?) - nable to parse file Assets/Game scene.unity.meta: [Control characters are not allowed] at line 0

1 Answers1

0

You can no longer access renderer that is inherited from MonoBehaviour directly. You must use GetComponent to get the Renderer. The-same thing applies to other components such as Rigidbody and AudioSource.

Renderer myRenderer;
public float speed = 0.5f;

void Start()
{
    myRenderer = GetComponent<Renderer>();
}

 // Update is called once per frame
 void Update()
 {
     Vector2 offset = new Vector2(0, Time.time * speed);
     myRenderer.material.mainTextureOffset = offset;
 }

Of-course, GetComponent<Renderer>().material.mainTextureOffset = offset; could have worked too but it is better to cache it like I did in the first script.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Also, as mentioned by `muXXmit2X`, it should be `Time.deltaTime * speed`. – Gunnar B. Oct 20 '16 at 11:56
  • @GunnarB. `Time.time` should be used for this because it increments each second. In order to move texture offset, you have to use a variable that increments or decrements constantly. That's not what `Time.deltaTime` is used for. `Time.deltaTime` is used for smoothing out values. `Time.time` is appropriate for [this](https://docs.unity3d.com/ScriptReference/Material-mainTextureOffset.html). – Programmer Oct 20 '16 at 13:08
  • @Adem, make sure to change `Texture Type` to `Texture` then change `Wrap Mode` to `Repeat`. More information [here](http://stackoverflow.com/a/36948841/3785314). – Programmer Oct 20 '16 at 13:12