Changing GameObject texture?

I'm making a "2D platformmer in a 3D world" like game, and the main character is a cube, with a sprite printed on it. The cube is very thin, so you cannot see the sides from the angle I'm rendering the game.

Anyway, I want to change the texture of the player when the player starts running, and when he stops. Here's the code I'm attempting to use:


var stillRight : UnityEngine.Material;
var stillLeft : UnityEngine.Material;
var movingRight : UnityEngine.Material;
var movingLeft : UnityEngine.Material;

function Update () {
    if (Input.GetButtonDown ("Right"))
        renderer.material.mainTexture = "movingRight";

    if (Input.GetButtonUp ("Right"))
        renderer.material.mainTexture = "stillRight";

    if (Input.GetButtonDown ("Left"))
        renderer.material.mainTexture = "movingLeft";

    if (Input.GetButtonUp ("Left"))
        renderer.material.mainTexture = "stillLeft";
}


I've associated the correct materials to the variables, but when I run the script, an error comes up the first time I attempting to change texture:

`InvalidCastException: Cannot cast from source type to destination type. Rotation.Update () (at Assets/Scripts/Character/Appearance/Rotation.js:8)`

Also, the texture doesn't change at all. Do any of you guys know how I can do this?

Ok I fixed the problem, I just changed the “OnGetButtonDown” in the moving left and right for “OnGetButton”, and added two bools to check in which direction the player is moving.

Here is the code, tested and working ;D:


var stillRight : UnityEngine.Texture;
var stillLeft : UnityEngine.Texture;
var movingRight : UnityEngine.Texture;
var movingLeft : UnityEngine.Texture;
var isWalkingLeft : boolean = false;
var isWalkingRight : boolean = false;

function Update () {
	//Moving Left 
    if (Input.GetButton("Left") && !isWalkingRight)
    {
        renderer.material.mainTexture = movingLeft;
        isWalkingLeft = true;    
    } else {
    	isWalkingLeft = false;
    }
    //Moving Right
    if (Input.GetButton("Right") && !isWalkingLeft)
    {
        renderer.material.mainTexture = movingRight;
        isWalkingRight = true;
    } else {
    	isWalkingRight = false;
    }
    //Still Left
    if (Input.GetButtonUp ("Left"))
        renderer.material.mainTexture = stillLeft;
    //Still Right
    if (Input.GetButtonUp ("Right"))
        renderer.material.mainTexture = stillRight;
}

You put quotation marks in the bottom. Those are strings, not textures. But your variables aren't textures, either. They're materials. So get rid of all of the .mainTexture's, too.

This will help your code break at compile time instead of runtime: http://forum.unity3d.com/threads/71445-How-To-Set-Project-Wide-pragma-Directives-with-JavaScript

Ok so your script could go like this:

var stillRight : UnityEngine.Texture;
var stillLeft : UnityEngine.Texture;
var movingRight : UnityEngine.Texture;
var movingLeft : UnityEngine.Texture;

function Update () {
    if (Input.GetButtonDown ("Right"))
        renderer.material.mainTexture = movingRight;

    if (Input.GetButtonUp ("Right"))
        renderer.material.mainTexture = stillRight;

    if (Input.GetButtonDown ("Left"))
        renderer.material.mainTexture = movingLeft;

    if (Input.GetButtonUp ("Left"))
        renderer.material.mainTexture = stillLeft;
}

What where you doing wrong was:

  • you were trying to remplace the player’s material main texture with another material, you had to remplace them with another texture so instead of using UnityEngine.Material, you have to use UnityEngine.Texture when declaring your variables.
  • you were adding " " in your if functions.

I tested it and it works, I hope this helps you :smiley:

Try changing the texture or material based on the objects transform, not input.