- Home /
How do I access a different material via Javascript?
Hi,
Sorry for the Newbie question!
I'm stuck on something really simple. I can't seem to change materials on an object. I'm trying ot change between the below materials on keypress "G". Both materials are in my materials folder, but l can't seem to get the script to load the "TestButtonOn" material, ...
Materials, ... TestButtonOff (already assigned) TestButtonOn
Code, ...
function Update ()
{
var newTexture : Texture;
newTexture = Resources.Load("TestButtonOn");
if(Input.GetKeyDown(KeyCode.G))
{
renderer.material.mainTexture = newTexture;
}
if(Input.GetKeyDown(KeyCode.B))
{
print ("My Texture is " + newTexture.name);
}
}
Cheers
This will (re)load TestButtonOn every frame, so like 60+ times per second. Do that in Start ins$$anonymous$$d. Did you read the manual here? http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
Thanks Dave, yes l did since work that out and fixed it with an function Start (). But the "TestButtonOn" material still doesn't load when l hit "G" ... ins$$anonymous$$d l just get an empty texture slot on the material?
..... "TestButtonOn" reports back as an 'un$$anonymous$$nown identifier'
Couple things:
Do you have actual materials or are these just textures? Your script is acting like they are textures, but your question as if they are materials.
From a performance standpoint, I would not be loading up that texture every time in update, I would move it into where you know the key has been pressed. Or you could load it up in Start and store it on the object and then flip to your other texture when the key is pressed.
Answer by Mukabr · Jun 10, 2013 at 05:04 PM
i dont know if that will work, but you can try
newTexture = Resources.Load("TestButtonOn", Texture);
as the Resources.Load receives 2 params, the path of the image and the type to be cast.
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
Your answer
Follow this Question
Related Questions
Apply a texture at startup js 1 Answer
change skybox via script help ? 1 Answer
Changing the tint colour of materials 1 Answer
Texture change script 0 Answers