newbie to unity and failing at the first hurdle

Hi Guys

I have downloaded Unity 5.5.1f1

I have been trying to follow the tutorial by Adam Buckner 2D Scolling Backgrounds

I have just created the scipt to scroll the material on the quad and when I try to run it, it gives this error

Assets/_Scripts/OffsetScroller.cs(18,26):
error CS0120: An object reference is required to access non-static member `UnityEngine.Renderer.sharedMaterial’

this is my copy of the script

using UnityEngine;
using System.Collections;

public class OffsetScroller : MonoBehaviour {

    public float scrollSpeed;


    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
        float y = Mathf.Repeat (Time.time * scrollSpeed, 1);
        Vector2 Offset = new Vector2 (0, y);
        Renderer.sharedMaterial.SetTextureOffset ("_MainTex", Offset);

    }
}

when I type the Renderer. monodevelop does not give any help filling in the sharedMaterial etc

has unity changed the way this is done ???, or did i miss some other setting ???

hi

Figured it out, yes the code is old and outdated, after going back to the location of the tutorial I noticed the code had been updated ( teach me to look first next time ). therefore did a quick copy/paste BUT then unity informed me that parts of the code was outdated and and gave the option to update the code. therefore the new code and it does work ( for now) is as follows:

using UnityEngine;
using System.Collections;

public class OffsetScroller : MonoBehaviour {
  
    public float scrollSpeed;
    private Vector2 savedOffset;
  
    void Start () {
        savedOffset = GetComponent<Renderer>().sharedMaterial.GetTextureOffset ("_MainTex");
    }
  
    void Update () {
        float y = Mathf.Repeat (Time.time * scrollSpeed, 1);
        Vector2 offset = new Vector2 (savedOffset.x, y);
        GetComponent<Renderer>().sharedMaterial.SetTextureOffset ("_MainTex", offset);
    }
  
    void OnDisable () {
        GetComponent<Renderer>().sharedMaterial.SetTextureOffset ("_MainTex", savedOffset);
    }
}

hopefully if someone else has the same problem then they will find the answer here ( unless they change it again )

1 Like

Use code tags, not red text if you want programmers to bother looking.

1 Like

sorry, I have changed it
I’m new here and didn’t know how to do it, also I posted in the wrong place, thanks to who ever moved it.

I know I don’t keep up on updates as well as others do, but did I slip into a coma and miss a couple releases? :eyes:

2 Likes