- Home /
How to add my texture to a cube?
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)
Answer by robertbu · Jul 02, 2013 at 02:23 PM
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.
Your right you don't see it because the script is linked to GameObject, but thx to your post i figured out what i did wrong.
I now drag and dropped the texture to that part where the arrow is and now it works fine. So thank you for the help.
You are absolutely right. I did not read your code carefully enough. I'm glad you solved your problem.
Answer by xKroniK13x · Jul 02, 2013 at 02:31 PM
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;
}
Your answer
Follow this Question
Related Questions
How do i make a cube invisible 2 Answers
Dynamically tiling texture on a cube of changing scale. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How would I implement this game mechanic? 0 Answers
How to display text on a cube and how to change it dynamically? 2 Answers