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 Solo8000 · Jun 25, 2018 at 11:55 PM · scripting problem

Material array not working,

Hi. I am having trouble with a project I've been working on. I have an object and I need it to change materials when the player collides with it. I followed a tutorial and it originally worked perfectly, but it just stopped registering the material array in the inspector one day and I can't work out why. All the collisions and everything works it is literally just the array. Here is my code:

 Material[] materials;
 private Renderer rend;

 void Start () 
 {
     rend = gameObject.GetComponent<Renderer>();
     rend.enabled = true;
     rend.sharedMaterial = materials[0];
 }

 private void OnCollisionEnter(Collision col)
 {
     if (col.gameObject.tag == "Player")
     {
         rend.sharedMaterial = materials[1];
     }
 }
Comment
Add comment · Show 1
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 wicke · Jun 26, 2018 at 01:13 AM 0
Share

Can you elaborate on what is not working? How are you assigning materials to the array?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Grocho · Jun 30, 2018 at 08:35 AM

The problem may be in a different script. See if something else is fiddling with the render or material.

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 Solo8000 · Jul 03, 2018 at 04:17 AM 0
Share

The object has no other scripts on it.

avatar image
0

Answer by brotar · Jul 03, 2018 at 06:34 AM

Changing material itself doesn't have an issue. Below code should work.

     public Material[] materials;
     private Renderer rend;
 
     void Start()
     {
         rend = gameObject.GetComponent<Renderer>();
         rend.enabled = true;
         rend.sharedMaterial = materials[0];
     }
 
     private void OnCollisionEnter(Collision col)
     {
         Debug.Log(col.gameObject);
 
         if (col.gameObject.tag == "Player")
         {
             rend.sharedMaterial = materials[1];
         }
     }

If the OnCollisionEnter function is not called, there are 2 reasons. First, the OnCollisionEnter function could not be called because of inappropriate collider&rigidbody setting. You can simply check it with Debug.Log() in OnCollisionEnter. About collision check this: https://docs.unity3d.com/Manual/CollidersOverview.html. Scroll down below and see collision action matrix. Second, It is not called if you disabled physics collision between layers or colliders. Go to Edit/Project Setting/Physics and check Layer Collision Matrix. Also, check if you are using Physics.IgnoreCollision or Physics.IgnoreLayerCollision. Hope this helps you.

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 Solo8000 · Jul 06, 2018 at 06:20 AM 0
Share

I should probably say that after playing with my script for a while I managed to get all the collisions working; when the player and ground collide the ground turns a strange purple colour. The problem is that the array is not registered in the inspector so I have no way to choose the materials I want.

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

95 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 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 can I get a variable from another player? 0 Answers

How to make Boolean not add more than one integer? 2 Answers

Destroy by Contact (OnTriggerEntry) - destroying more then i want. 0 Answers

Script won't execute on assigned 3d object but on simple cube 0 Answers

How to detect an object which be in FOV of certain camera ? 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