x


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?

more ▼

asked May 14 '11 at 10:28 PM

Biendeo gravatar image

Biendeo
127 12 13 24

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
}
more ▼

answered Jun 13 '11 at 03:22 AM

BeatCord gravatar image

BeatCord
16 3 3 5

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 14 '11 at 11:41 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

Thankyou for your answer, but I have one more problem now. The player can mess us the texture by pressing both the keys at once. Is there a way to prevent that?

May 16 '11 at 11:16 AM Biendeo

Run different delegates ("Function types") depending on state. http://unity3d.com/support/documentation/Manual/MonoUpgradeDetails.html

May 16 '11 at 12:58 PM Jessy

I'm not quite sure what you mean from that. I don't really know how to implement that into the script. Can you help me?

Jun 11 '11 at 11:26 PM Biendeo
(comments are locked)
10|3000 characters needed characters left

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 :D

more ▼

answered Jun 12 '11 at 01:04 AM

BeatCord gravatar image

BeatCord
16 3 3 5

I like your script, but there's one thing that I think should be fixed. Say you're running right. Pressing left while running right will change the texture to the movingLeft, and then changes it to the stillLeft when you let go of the left key, all while moving right. Do you know how to fix this?

Jun 12 '11 at 01:25 AM Biendeo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3337
x2207
x1047
x819
x380

asked: May 14 '11 at 10:28 PM

Seen: 5978 times

Last Updated: Jun 13 '11 at 03:26 AM