- Home /
Randomly select and apply a texture?
What we're attempting to do is to randomly select a texture from a folder of textures in the asset list, and apply that texture to a game object. No matter what we try the texture doesn't seem to get applied, even if we forgo randomly generating the number and specify a specific texture it still doesn't apply. I feel like if we can figure out how to apply a texture programmatically we can get the rest to work but even after consulting the unity script reference we're still pretty stuck. here is the code we are working with so far.
using UnityEngine;
using System.Collections;
public class TestTexture : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject.Find("Planet_3").renderer.material.SetTexture ("_MainTex",
(Texture)Resources.Load ("Assets/Standard Assets/Terrain Assets/Planet Textures/0"));
}
// Update is called once per frame
void Update () {
GameObject.Find("Planet_3").renderer.material.SetTexture ("_MainTex",
(Texture)Resources.Load ("Assets/Standard Assets/Terrain Assets/Planet Textures/0"));
}
}
its supposed to work by generating a random number (all of the textures are named for numbers) and then to use that number to select the desired texture and apply it to the game object.
can anyone tell us what we're doing wrong, and recommend how to fix it?
Have you seen this? http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
Do you really want to load a texture every frame?
Are there any errors or warnings in the console?
yes, when i said that we had "[consulted] the unity script reference" that is the exact page to which i was referring.
no, that was us debugging. we figured maybe the start just wasn't applying for whatever reason and so we threw it into the update to see if it would make a difference. it did not.
we have the warning "The Referenced Script on this Behavior is missing!" several times.
Answer by aldonaletto · Oct 11, 2013 at 04:08 AM
@DaveA is right: the page linked says that the resources must be in Resources folders - you specify the resource name, not its path, and Resources.Load searches in all Resources folders. You're not getting error messages because SetTexture doesn't throw any exception when the texture is null - it just does nothing. Move the textures to a Resources folder and just pass the asset name ("0", in this case).
The rest of the code seems ok - I tested the simple script below and the textures were randomly changed every second:
var textures: Texture2D[]; // assign some textures here in the Inspector
function Start (){
while (true){
renderer.material.SetTexture ("_MainTex", textures[Random.Range(0, textures.Length)]);
yield WaitForSeconds(1.0);
}
}
NOTE: The error message you've got says that a script attached to some object was deleted, what has nothing to do with the texture problem.
Thanks Aldonaletto - this works well.
I call the random material to apply to a random ball generator I wrote and my balls now have different textures.
Your answer
Follow this Question
Related Questions
Randomly floating GUI Texture 1 Answer
Assigning UV Map to model at runtime 0 Answers
How can I make a good-looking path? 0 Answers
Animated Rotten Tomato 0 Answers