Scripting: Change Material of an Object | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to today's tutorial today we're going to be learning how to change the material of an object um through scripting uh so you may need this um in your game if you want a character to have different material when it clicks on an object um or really anything and it's a it's pretty simple to do but it took me a while to figure out um exactly how to do it so I thought I'd make a tutorial just to make this easy if you're trying to figure out uh how to do this so uh let's get into it so here we are in unity um and what we have is we have a little Cube here a red one and a green one and a plane basically what we're going to do is we're going to give this uh a rigid body so it falls collides with the cube when it collides it'll change color um and then when it collides with the ground it'll change a different color as well um so first let's just get it falling let's add a rigid body um and make sure we have a box collider on uh and so we do so that'll be fine uh and we'll just go ahead and give it a play Nothing should happen it'll just Nick off it'll just hit the uh Nick off that's a it'll just hit the green box and fall so let's gock play again goes bam okay perfect um so I've added a script here it has nothing on it just says change material it's just a blank script um so let's start uh editing this real quick so what we're going to need in order to access our material is we're going to need to make a public material um and we're going to make it an array so we can have multiples and we're just going to call it material lowercase so we have this variable here called material this is an array it allow us to access multiple materials so we could have um any number of materials that we want to change our object to next we're going to make uh a renderer um this is just going to access your game object you can make it public um we're just going to leave it private you can type private or you can just put it in we're going to put renderer um and we're just going to call it Rend okay so on our start right here um as soon as our game object enters uh we need to get a couple things first we're going to make a Rend um equal um the renderer of our object so how we do that is we do get component um and we're accessing the renderer in here right here okay so we're making our renderer equal um the game objects renderer itself um you don't have to necessarily do this but just to make sure it's enabled you can do ren. enabled equals true so that it will be enabled um that's just in case it's not so next uh we're going to set the material of the render we do Rend do shared material equals material um and then in Brackets put the number you want so we have there we go zero so this is just going to ask access the first material um in materials up here uh this is our variable and it's just accessing the first one in the array so we're going to get rid of update because we're not going to need it and we're going to add an on collision enter so do void on collision enter um in Brackets we need to make a variable uh Collision variable we just going to call it coal uh so make brackets there and here we go so when um our object collides we want something to happen um now what we need to do in order for it to uh know that it collides with the cube we need to set up something real quick so let's go back into our game here um let's take this green cube right here we're going to actually give it a tag um we're going to do add tag um make one and we're just going to call it box um and then go back to your green Cube add this tag called box um and we're going to access it back in our script here so let's go back to edit this thing so if cole. game object. tag equals box um then we're going to change the material right here the first material um so what Cole is right here is actually the object it's uh hitting so whatever object it hits will be named Cole um and we're just making sure that the name of its tag is box so once it does that it will change its material um and if it doesn't we'll just do else uh we're going to change the material to the third material in our list which should be number two so that's how you change the material and then let's take a look at it all right so once we've saved it um we need to set the size of our array it's only three um and let's go ahead and pick our materials so at the beginning it'll be red when it hits it'll be green um and when it falls to the floor it'll be blue so let's click play and take a look at it there we go and that's how you change materials in unity through a script all right well I hope this tutorial was helpful um if you did find it helpful please leave a like or a comment if you need anything else to know um but other than that uh thank you for watching bye
Info
Channel: Rosscoe Tutorials
Views: 138,208
Rating: undefined out of 5
Keywords: Unity, unity3d, material, script, coding, c#, Change material
Id: dJB07ZSiW7k
Channel Id: undefined
Length: 5min 31sec (331 seconds)
Published: Wed Apr 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.