Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 08, 2015 at 09:57 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Biendeo · May 14, 2011 at 10:28 PM · texturematerialcharacterchange

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alone1992 · Feb 08, 2015 at 08:45 AM 0
Share

thanks dear

avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Biendeo · May 16, 2011 at 11:16 AM 0
Share

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?

avatar image Jessy · May 16, 2011 at 12:58 PM 0
Share

Run different delegates ("Function types") depending on state. http://unity3d.com/support/documentation/$$anonymous$$anual/$$anonymous$$onoUpgradeDetails.html

avatar image Biendeo · Jun 11, 2011 at 11:26 PM 0
Share

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?

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Biendeo · Jun 12, 2011 at 01:25 AM 0
Share

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?

avatar image
0

Answer by jimjames · Dec 22, 2014 at 01:44 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges