I tried to write a code that would add a texture to a cube
var BlueColorTex: Texture2D;
function Start () {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.renderer.material.mainTexture = BlueColorTex;
cube.transform.position = Vector3 (0.5, 0.5, 0.5);
}
function Update () {
}
The code gives no errors,
But it also doesn’t apply my texture.
The cube stays the same, but it does show it tried to apply the texture.
What to do?
(this is my first post here i hope i added all i need to)
Looking at your image, I don’t see any script attached to the cube. Did you perhaps attach the script while Unity was running? This is a mistake I still occasionally make. Note after attaching your script, you will have to drag and drop your texture onto the ‘BlueColorTex’ variable in the inspector.
Make sure the Materials drop down on the cube has 1 material in it, and not 0.
The way I have it setup in my game (a nearly identical command) is to have the cube as a Prefab with all components needed on it (Mesh Renderer with 1 material, rigid body if needed, etc). And then I have this script:
var brick : GameObject; //Assign this to be your prefab
var blueColorTex; //Assign this to be your texture
function Start(){
var cube = Instantiate(brick, Vector3 (0.5, 0.5, 0.5), Quaternion(0,0,0,0));
cube.renderer.material.mainTexture = blueColorTex;
}