Search Unity

API update Renderer error assistance!

Discussion in '2D' started by joplrw10, Aug 1, 2015.

  1. joplrw10

    joplrw10

    Joined:
    Aug 1, 2015
    Posts:
    5
    I have been looking for a thread that would help with texture offset on an object, but I have come across something that is not in the current API.

    public class ScrollScript : MonoBehaviour {
    public float speed = 0;

    void Update () {
    renderer.material.mainTextureOffset = new Vector2 (Time.time*speed,0f);
    }
    }


    I have attempted to change this to

    public class ScrollScript : MonoBehaviour {
    public float speed = 0;
    static Renderer rend = GetComponent <Renderer>();
    // Use this for initialization
    void Start () {

    }

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

    But I am getting An object reference is required for a non-static field, method, or property 'UnityEngine.Component.GetComponent<T>()' error.

    New to scripting so I feel like I am missing something simple!
     
  2. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    436
    Code (CSharp):
    1. public class ScrollScript : MonoBehaviour
    2. {
    3.     Renderer rend;
    4.     // Use this for initialization
    5.     void Start ()
    6.     {
    7.         rend = GetComponent <Renderer>();
    8.     }
    9. }
    10.