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 /
avatar image
0
Question by W3ST · Dec 28, 2010 at 04:43 PM · texturechange

Changing Object Texture When button is pressed.

i found a script, which when a button is pressed, the texture of an object changes:

var texture1:Texture2D;

function Update () { if(Input.GetButton("Forward") ) {

if(texture1) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }

else { Debug.Log("Unable to Load texture..."); } } }

however i need this feature, but more than one texture on more than one button. I made the following changes:

var texture1:Texture2D;

var texture2:Texture2D; var texture3:Texture2D; var texture4:Texture2D;

function Update () { if(Input.GetButton("Forward") ) {

if(texture1) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }

else { Debug.Log("Unable to Load texture..."); } } if(Input.GetButton("Backward") )

{

if(texture2) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }

else { Debug.Log("Unable to Load texture..."); } } if(Input.GetButton("Left") )

{

if(texture3) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }

else { Debug.Log("Unable to Load texture..."); } } if(Input.GetButton("Right") )

{

if(texture4) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; }

else { Debug.Log("Unable to Load texture..."); } } }

however this doesnt work properly. Any ideas how i can fix 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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ejlersen · Dec 28, 2010 at 05:54 PM

Well, I think this has been answered a few times here, and efge's answer is one simple way of doing it. Here is another example (basically the same as efge's):

// Cache our renderer private var rend : Renderer;

// Store the textures in an array. Then you can quickly and more or remove some public var textures : Texture2D[];

// Awake function Awake() { // Get the renderer rend = gameObject.GetComponent(Renderer);

// If none, then add one if (rend == null) rend = gameObject.AddComponent(MeshRenderer); }

// Update function function Update() { if (Input.GetButton("Forward")) ChangeTexture(0); else if (Input.GetButton("Backward")) ChangeTexture(1); else if (Input.GetButton("Left")) ChangeTexture(2); else if (Input.GetButton("Right")) ChangeTexture(3); }

// Change texture function ChangeTexture(inx : int) { // Just make a small check, that we don't try to take a non-existing texture if (inx >= textures.Length) return;

// Change texture rend.material.mainTexture = textures[inx]; }

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 BigBlob · May 30, 2012 at 03:08 PM 0
Share

a quick question, since you have there ChangeTexture(0); i wonder if you an set a var type so the color would not be random

avatar image
0

Answer by efge · Dec 28, 2010 at 05:27 PM

You have to assign the right var, e.g.:

renderer.material.mainTexture = texture1; 

(not renderer.material.mainTexture = Texture; )


And maybe it would be better to make use of arrays, like this:

var MyTextures : Textrue2D[];

...

for (i=0; i< MyTextures.Length, i++) if (Texture[i]) Debug.Log("Texture" + i " Loaded Sucessfully...");

switch (Input.GetButton) { case : "Backward"; renderer.material.mainTexture = Texture[0]; break; case : ...

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
avatar image
0

Answer by castleforce · Oct 13, 2011 at 01:32 AM

Hey, is there a way to make this for materials and not textures

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Changing GameObject texture? 4 Answers

Change texture of GUItexture from script 4 Answers

Change/load a mesh texture 2 Answers

Adding Fog 3 Answers

Creating a component from a button click 1 Answer


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