Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

newbie to unity and failing at the first hurdle

Discussion in 'Scripting' started by KevG, Oct 4, 2015.

  1. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OffsetScroller : MonoBehaviour {
    5.  
    6.     public float scrollSpeed;
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         float y = Mathf.Repeat (Time.time * scrollSpeed, 1);
    17.         Vector2 Offset = new Vector2 (0, y);
    18.         Renderer.sharedMaterial.SetTextureOffset ("_MainTex", Offset);
    19.  
    20.     }
    21. }
    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 ????
     
    Last edited: Oct 4, 2015
  2. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OffsetScroller : MonoBehaviour {
    5.  
    6.     public float scrollSpeed;
    7.     private Vector2 savedOffset;
    8.  
    9.     void Start () {
    10.         savedOffset = GetComponent<Renderer>().sharedMaterial.GetTextureOffset ("_MainTex");
    11.     }
    12.  
    13.     void Update () {
    14.         float y = Mathf.Repeat (Time.time * scrollSpeed, 1);
    15.         Vector2 offset = new Vector2 (savedOffset.x, y);
    16.         GetComponent<Renderer>().sharedMaterial.SetTextureOffset ("_MainTex", offset);
    17.     }
    18.  
    19.     void OnDisable () {
    20.         GetComponent<Renderer>().sharedMaterial.SetTextureOffset ("_MainTex", savedOffset);
    21.     }
    22. }

    hopefully if someone else has the same problem then they will find the answer here ( unless they change it again )
     
    Last edited: Oct 4, 2015
    GroZZleR likes this.
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Use code tags, not red text if you want programmers to bother looking.
     
    Kiwasi likes this.
  4. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    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.
     
  5. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    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? o_O
     
    LargerEagle and Kiwasi like this.