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 Archviz3d · May 20, 2017 at 03:10 AM · scripting problemmaterialgameobjectsfindshare

How to find all gameobjects that use the same material and change it?

Hi! Im a noob at programming, and im having some trouble with this simple task...

What i wanted is to find all gameobjects that use a specific material, and change it to another different material.

So in the script ive done, i have to put by hand the material i want to find and the material i want it to change to, and then in play time when i press space it was supposed to change to the new material but its not happening.

Could someone help me?

Thank you so much!!!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Script_ChangeMaterials: MonoBehaviour
 
 {
 
     public Material materialtochange;
     public Material materialchanged;
        private Renderer rend;
        private Material[] mats;
     
     void Update()
     {
         if (Input.GetKeyDown("space")) 
         {
             Renderer[] rend = FindObjectsOfType(typeof(Renderer)) as Renderer[];
 
 
             for( int i = 0 ; i < rend.Length ; i++)
             {
                 if(rend[i].material == materialtochange)
                 {
                     rend[i].material =  materialchanged;
                 }
               }
         }
     }
 }
 
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 toddisarockstar · May 20, 2017 at 04:16 AM 0
Share

its a bit wierd. I went through this. you have to create a variable for the material in your script. then drag the shared material into the inspector. you manually change the texture and settings of the material in use to get your result. Changing which material the variable is holding wont do anything for you. and if you try to get a metrial from an object during runtime, it just makes an new material instance and won't change any other objects. so dragging and dropping in the inspector is the only way to edit a material that everything is using. you dont use a different material. you just change it to your needs. unless you want to manually find every object in the scene that is using it.

careful though. doing this can permanently change your material setting in the asset folder. so back up your material folder before playing with any code like this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DarthHawk13 · May 20, 2017 at 04:08 AM

I believe a way to do this is search through all GameObjects in the scene looking for the material you want to replace on each individual GameObject. When a GameObject has that material you're looking for then replace the material on that GameObject.

The code looks like you want to search for all the Renderers in the scene so you can change the material attached to them but each Renderer is a component of a Gameobject. I believe you need to access each GameObject's Renderer through each individual GameObject and see if the material on the Renderer of each GameObject matches the material you want to replace. Then you can replace it.

You may also be getting an error from declaring rend to be a Renderer at the beginning of your code and later on line 18 trying to make it an array when you didn't declare it as an array on line 11.

This Unity Documentation link will get you closer to achieving your goal:

https://unity3d.com/learn/tutorials/modules/beginner/scripting/scripts-as-behaviour-components?playlist=17117

Go through the videos and scripts of the aspects of code you may not be familiar with. It looks to me you're headed on the right path from the way you're thinking about the problem but may need to freshen up on syntax and how Unity and GameObjects are structured. Going through the beginner and intermediate script tutorials may take a little while but it should be shorter than going through a C# book. As you mentioned you're new to programming going through a C# book would be a rewarding experience also. But you don't have to do all that before solving your problem. You can solve your problem in short order by going through some of the script tutorials.

And don't be afraid of getting things wrong or get frustrated if something's not compiling. When I started out it took me three hours to discover I had a missing closing bracket. That ruined my day because I was sooo frustrated and I didn't need to be.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop? 0 Answers

Creating Material from String (Unity 5.6) 1 Answer

Gather all GameObjects with a certain name in a Scene and Apply them to a list? 3 Answers

Need Help with changing skyboxes -1 Answers

Change Material depending on Collider position? 2 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