Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Cervelx · Sep 28, 2016 at 09:35 PM · scripting problem

i need a script to put an albedo into a material

Hi guys ! Please help me, i follow the first tutorial and i write this script

using UnityEngine; using System.Collections;

public class ExampleBehaviourScript : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.R)) { GetComponent ().material.color = Color.red; } if (Input.GetKeyDown(KeyCode.G)) { GetComponent().material.color = Color.green; } if (Input.GetKeyDown(KeyCode.B)) { GetComponent().material.color = Color.blue; } } }

It steel work ok, but i need to change the albedo texture, not just the color, I need to put my texture into the material via script, it is possible ?alt text

script-example.jpg (159.6 kB)
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

2 Replies

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

Answer by TBruce · Sep 28, 2016 at 10:46 PM

Yes it is possible. Lets say you have two variables (I will make them public so they can be set through the inspector) like this

 public Sprite sprite;
 public Material material;

You can then set the Albedo image using the following code

 if ((sprite != null) && (material != null))
 {
     material.SetTexture("_MainTex", sprite);
 }
Comment
Add comment · Show 7 · 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 TBruce · Sep 29, 2016 at 01:55 AM 1
Share

@Cervelx Did this work for you?

avatar image Cervelx TBruce · Sep 29, 2016 at 07:01 PM 0
Share

@$$anonymous$$avina uff it soesen't work :( i have this errors: 1-Assets/Scripts/BallSkin.cs(19,34): error CS1503: Argument #2' cannot convert UnityEngine.Sprite' expression to type UnityEngine.Texture' 2-Assets/Scripts/BallSkin.cs(19,34): error CS1502: The best overloaded method match for UnityEngine.$$anonymous$$aterial.SetTexture(string, UnityEngine.Texture)' has some invalid arguments

and this is the script: using UnityEngine; using System.Collections;

public class BallSkin : $$anonymous$$onoBehaviour {

 public Sprite sprite;
 public $$anonymous$$aterial material;

 // Use this for initialization
 void Start () {

 }
 
 // Update is called once per frame
 void Update () {

     if ((sprite != null) && (material != null)) 
     {
         material.SetTexture("_$$anonymous$$ainTex", sprite);
     }

 
 }

}

avatar image TBruce Cervelx · Sep 29, 2016 at 07:05 PM 1
Share

@Cervelx Sorry I did not have time to test my script. This

 material.SetTexture("_$$anonymous$$ainTex", sprite);

should have been this

 material.SetTexture("_$$anonymous$$ainTex", sprite.texture);
Show more comments
avatar image Cervelx · Sep 29, 2016 at 12:01 PM 0
Share

Thank youuuuuuuuuuuu :D

avatar image
0

Answer by Cervelx · Sep 29, 2016 at 07:49 AM

Thank you Thank you Thank you Thank you Thank you !!! Tonight i will test it :D

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

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save Item from List in Unity 2 Answers

Can I change animation key frame property in script? 1 Answer

Unturned death barrier on objects 1 Answer

Sphere wont load the script 0 Answers

The Issue with 'Func' 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