• 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 /
avatar image
-1
Question by Krshojestic · Aug 19, 2013 at 07:00 PM · drawing

Draw 3D objects in code

My ultimate goal is to draw a house through code. Not something fancy, just a couple of walls and a roof will do for a start. Additionally, I want to be able to add windows to the walls. What's the best way to do this?

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

· Add your reply
  • Sort: 
avatar image
0

Answer by ThomasB · Aug 19, 2013 at 07:29 PM

I recommend you take a look at this : http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html . You can change the code examples to C# by click on the drop-down box located at the top left of the page.

Although I would recommend you use a 3D modelling package to create something like a house. This could be useful if you prefer to make it by modelling : http://www.blender.org

Here is a starting point you could use. Make sure you attach this to a game object:

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
     void Start() {
         Mesh mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
     }
 }
Comment
Add comment · Show 7 · 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
avatar image Krshojestic · Aug 19, 2013 at 08:14 PM 0
Share

I'm sorry, but it's not specific enough for me. I mean I've worked with Unity before, but not with this. I took a look at the first link you provided and I started by copying the example code provided there (it's in JS there, but I translated it into C#) and this is the runtime error I got: "object reference not set to an instance of an object" on this line of code:

 GetComponent<$$anonymous$$eshFilter>().mesh = mesh;
avatar image roojerry · Aug 19, 2013 at 08:18 PM 0
Share

If you are looking to make something specific like a house, why not use a 3D modeling program like @$$anonymous$$B mentioned above. Trying to build a mesh procedurally is not an easy task for a beginner and unless you have some need for generating this house on the fly during runtime, save yourself the trouble and just build the model outside of unity and importing it in as an asset.

avatar image ThomasB · Aug 19, 2013 at 08:20 PM 0
Share

Could you paste the whole block of code so I can try and identify the problem? Thanks. It could be due to the fact that the variable mesh wasn't declared before this line of code. Although, further to what $$anonymous$$ said I would strongly recommend following that method.

avatar image robertbu · Aug 19, 2013 at 08:24 PM 0
Share

@$$anonymous$$rshojestic - You will get an error if you attach that code to an empty game object. Attach it to a game object with an existing mesh (like a cube), and it will work.

Unity does not make your goal here easy. Assu$$anonymous$$g you want to create your house out a mesh, you might be best to work through some examples. Here is one link:

http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/

avatar image CHPedersen · Aug 19, 2013 at 09:02 PM 1
Share

I recommend you study the Procedural Examples provided by Unity Technology themselves. You can download the package here:

http://u3d.as/content/unity-technologies/procedural-examples/3zu

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is the System.Drawing namespace not supported in Unity 3.0 Beta? 1 Answer

What is the best way to create a polygon shape in Unity? 3 Answers

Code for drawing basic 2D primitives on Texture2D 1 Answer

White outline around tree leaves after upgrade to Unity 5 1 Answer

Drawing and saving during gameplay 0 Answers


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