Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 02, 2012 at 01:49 AM by Eric5h5 for the following reason:

Duplicate Question

avatar image
0
Question by MinecraftUnity · Jun 02, 2012 at 01:32 AM · javascripttexturecubeminecraft

How do I put a texture on my Cube using code (JavaScript)

I am completely new to Unity 3D. I own the free version. For my first project, I am trying to make a MineCraft like clone. I followed tutorials, but now i'm trying to change something so it works how I want it. First off, how can I place a cube with a texture on it? Here's a chunk of code where I need to be able to do this:

 function Build() { 
     if (HitBlock()) { 
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 
         cube.transform.position = hit.transform.position + hit.normal; 
     } 
 }
 

This spawns a default cube with no texture whatsoever. How do I get a texture?


Here is my entire code ( I followed tutorials, so I shouldn't be saying mine):

 var blockLayer : LayerMask = 1; 
 var range : float = Mathf.Infinity; 
 var hit : RaycastHit;
 
 function Update () { 
     if (Input.GetMouseButtonDown(1)) Build(); 
     if (Input.GetMouseButtonDown(0)) Erase(); 
 }
 
 function Build() { 
     if (HitBlock()) { 
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 
         renderer.material.mainTexture = Resources.Load("Dirt.png");
         cube.transform.position = hit.transform.position + hit.normal; 
     } 
 }
 
 function Erase() { 
     if (HitBlock()) 
     Destroy(hit.transform.gameObject); 
 }
 
 function HitBlock() : boolean { 
     return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer); 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by Berenger · Jun 02, 2012 at 01:44 AM

This should already work if Dirt.png can be found. However, note that Resources.Load is an expensive function. You must call only when you really have to and certainly not several times for the same resource. declare a Texture2D in your script and set it up in the inspector or, if you really can't do otherwise, call Load at Start and store the value.

Now that we worked that out, it appears that minecraft worlds are not thousands of cube, 24 vertices and all that stuff. I've never looked into it, but it seems more complicated than that. Usefull documentation :

http://forum.unity3d.com/threads/63149-After-playing-minecraft...

http://forum.unity3d.com/threads/69573-MinePackage-Minecraft-starter-package?highlight=Procedural

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a Cube with a texture in a JavaScript? 0 Answers

Primitive Cube Texturing Help 3 Answers

Apply texture to a bar (cube) 2 Answers

How to add my texture to a cube? 2 Answers

background picture in my game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges