- Home /
The question is answered, right answer was accepted
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?
Answer by BeatCord · Jun 13, 2011 at 03:22 AM
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;
}
Answer by Jessy · May 14, 2011 at 11:41 PM
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
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?
Run different delegates ("Function types") depending on state. http://unity3d.com/support/documentation/$$anonymous$$anual/$$anonymous$$onoUpgradeDetails.html
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?
Answer by BeatCord · Jun 12, 2011 at 01:04 AM
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
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?
Answer by jimjames · Dec 22, 2014 at 01:44 AM
Try changing the texture or material based on the objects transform, not input.
Follow this Question
Related Questions
How to create a GUI button to change a character's texture in real-time? 2 Answers
Errors while assigning material to other object's by scripting. 1 Answer
Best option to change texture/material.. 0 Answers
How to switch texture of several 3D objects on GUI buttons click? 1 Answer
Character Material Layers 0 Answers