- Home /
How to change the textures of assigned gameobjects?
Hello I have a script with this script you can change a texture by putting A link in with an image. This must change te textures of als the "boxes" because they heve the same begin texture but this is not true. this is the script:
//this is not a valid URL the player can change this i will change it to.
var url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
var Box = GameObject;
function Start () {
// Create a texture in DXT1 format
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
while(true) {
// Start a download of the given URL
var www = new WWW(url);
// wait until the download is done
yield www;
// assign the downloaded image to the main texture of the object
www.LoadImageIntoTexture(renderer.material.mainTexture);
}
}
function OnGUI () {
url = GUI.TextField (Rect (10, 10, 200, 20), url, 500);
}
What i will is that I can give a name and that all the gameobject with that name the picture from the link as texture uses.
P.S sorry for the bas english
If I understand what you are asking correctly, you want to use 'renderer.shared$$anonymous$$atherial' ins$$anonymous$$d of 'renderer.material'. Note in the editor this will result in a permanent change.
Answer by alexeyzakharov · Jul 09, 2014 at 03:11 PM
As robertbu pointed out you should rather use renderer.sharedMaterial to change the texture of the common material for all boxes.
When you access renderer.material, the material gets instantiated and you work with its copy, so every box will use its own copy of the material. See http://docs.unity3d.com/ScriptReference/Renderer-material.html
No this is not the thing I have the same thing as renderer.material
this is the script now:
//this is not a valid URL the player can change this i will change it to.
var url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
var Box = GameObject;
function Start () {
// Create a texture in DXT1 format
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
while(true) {
// Start a download of the given URL
var www = new WWW(url);
// wait until the download is done
yield www;
// assign the downloaded image to the main texture of the object
www.LoadImageIntoTexture(renderer.shared$$anonymous$$aterial.mainTexture);
}
}
function OnGUI () {
url = GUI.TextField (Rect (10, 10, 200, 20), url, 500);
}
Your answer
Follow this Question
Related Questions
how to change the maintexture from another scene? 1 Answer
simple texture question 1 Answer
Texture/Object not visible 0 Answers
Assigning a texture to a renderer 0 Answers
How to tell if two blocks are right next to each other?(2D) 1 Answer