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 ChinChinati · Dec 27, 2016 at 02:44 PM · c#material renderer

Material changes when i drag and drop it on object but doesnt through code?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class swaptexture : MonoBehaviour {
 
     public gemlife g;
     public Material[] mats;
 
     private Renderer rend;
 
     // Use this for initialization
     void Start () {
 
         rend = GetComponent<Renderer> ();
         rend.sharedMaterial = mats [0];
     }
 
     // Update is called once per frame
     void Update () {
 
         if (g.ratio >= 0.96f)
             rend.sharedMaterial = mats [1];
     }
 }

when i drag and drop the materials on my player it works but when i use this script it doesnt. yes the condition is being met and code is working fine and i can also see the material changing inside the inspector but it doesnt reflect to my player :(

Comment
Add comment · Show 2
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 Hellium · Dec 27, 2016 at 02:35 PM 0
Share

Ins$$anonymous$$d of shared$$anonymous$$aterial , have you tried material ?

avatar image ChinChinati Hellium · Dec 27, 2016 at 02:39 PM 0
Share

yes but same result it didn't help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IgorAherne · Dec 27, 2016 at 04:23 PM

Your code seems fine.

I would debug it through breakpoints or you could also test out with several debug.Log()

  1. Check Update is called

  2. Check the name of this.gameObject

  3. check the name of rend.sharedmaterial

  4. check all the names in mats[]

  5. check the name of rend.sharedmaterial.shader

  6. check the name of shaders in mats[]

  7. try changing color in mats[1], then debug the color of rend.sharedmaterial = mats[1]

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 ChinChinati · Dec 27, 2016 at 04:31 PM 0
Share

i already confirmed that the update is being called since the condition is being met and moreover i can also see the material being changed in the inspector during runtime when the condition was met but for some reason it didnt reflect on my player. although when i drag and drop the same material on my player directly it works fine.

avatar image IgorAherne · Dec 27, 2016 at 04:49 PM 0
Share

Also, there might be several shared materials per model. What might happen is you could have 1 material for the body, and another for a tiny peice like belt-buckle. Try seeing how many shared materials you have (most likely there is an array), then assign your mats[1] into the correct slot of rend.sharedmaterials[]

https://docs.unity3d.com/ScriptReference/Renderer-shared$$anonymous$$aterials.html

avatar image ChinChinati IgorAherne · Dec 27, 2016 at 04:55 PM 0
Share

i only have one single texture for the whole body so there are only two of them: one is blue outfit and the other is yellow. the main problem here is that the material gets added to the object and it shows that it is changing inside the inspector but it NEVER GETS APPLIED to my object. i want the exact behavior which i get when i drag and drop that texture on my player...

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers

Changing Video Cam Texture Material With Buttons 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