Search code examples
c#unity-game-enginerenderer

unity renderer.material.mainTextureOffset.x not working


Hi everybody I am having a problem with my game, the problem is based when I switch the materials so it can face the other direction (it is a 2d game). This affects the animation which is controlled by the renderer.material.mainTextureOffset but I have no idea why this is not working.

code in c#:

using UnityEngine;
using System.Collections;

public class character : MonoBehaviour {
    public float forwardSpeed = 10.0f;
    public Material newMaterialRefcs1;
    public Material newMaterialRefcs2;
    // Use this for initialization
    void Start () {

    }
    //error is occurring here !!!!!!!! below Important. The 2 if statements below
    // Update is called once per frame
    void Update () {
    if( Input.GetKey(KeyCode.D)){
            renderer.material.mainTextureOffset = new Vector2(0.25f, 0);
            transform.position += -transform.right * forwardSpeed * Time.deltaTime;
                renderer.material = newMaterialRefcs1;

    }if( Input.GetKey(KeyCode.A)){
            renderer.material.mainTextureOffset = new Vector2(0.25f, 0);
            transform.position += transform.right * forwardSpeed * Time.deltaTime;
                renderer.material = newMaterialRefcs2;

    }

}}

Solution

  • I have figured it out with some help from the unity forums took a while but here it is:

    using UnityEngine;
    using System.Collections;
    
    public class character : MonoBehaviour {
        public float forwardSpeed = 20.0f;  public float rot = 0f;public float jumpSpeed = 100;public float gravity = 30f;
        public Material newMaterialRefcs1;
        public Material newMaterialRefcs2;
    
        void Start () {
    
        }
        public float scrollSpeed = 0.25F;
         void Update () {
        if( Input.GetKey(KeyCode.RightArrow)){
                scrollSpeed += 0.25f;
                transform.position += -transform.right * forwardSpeed * Time.deltaTime;
                renderer.material = newMaterialRefcs1;
                float offset = scrollSpeed;
            renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
        }if( Input.GetKey(KeyCode.LeftArrow)){
                scrollSpeed += 0.25f;
                transform.position += transform.right * forwardSpeed * Time.deltaTime;
                renderer.material = newMaterialRefcs2;
                float offset = scrollSpeed;
            renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
    
        }
            Vector3 isgrounded = transform.TransformDirection(Vector3.up);
            if( Input.GetKeyDown(KeyCode.Space)&& Physics.Raycast(transform.position, isgrounded, 6)){
                transform.position -= transform.up * jumpSpeed * Time.deltaTime*2;
    } 
             Physics.gravity = new Vector3(0, gravity, 0);
            transform.rotation = Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z);
    }
    }