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 /
avatar image
0
Question by CladInShadows · Mar 17, 2015 at 03:40 PM · gameobjectinstantiatemeshrendererfilter

Can't render GameObject

Hi,

I am trying to define and render a gameobject entirely in C# script (i.e. without an existing prefab).

The relevant part of my code is as follows:

     HexTile = new GameObject("Single Hex Tile");

     MeshFilter HexMeshFilter = HexTile.AddComponent<MeshFilter>();
     MeshRenderer HexMeshRenderer = HexTile.AddComponent<MeshRenderer>();
     
     Mesh HexMesh = new Mesh();
     HexMesh.vertices = HexVertices;
     HexMesh.triangles = HexTriangles;
     HexMesh.uv = HexUV;

     HexMesh.RecalculateNormals();
     
     HexMeshFilter.mesh = HexMesh;
     HexMeshRenderer.material.mainTexture = HexTexture;

     Instantiate(HexTile);

The rest of the code just defines the vertices, triangles, and uv. The code is attached to a blank gameobject. However, running it just leaves me with a blank screen.

I'm still very uncertain on the whole process of attaching a render and filter to my object and instantiating it.. so was hoping that someone would be able to point out the (probably obvious) thing I am doing wrong?

Thank in advance!

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Mar 17, 2015 at 03:41 PM 1
Share

You'll need a transform.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by maccabbe · Mar 17, 2015 at 06:30 PM

When you use new GameObject() you have already instantiated a new empty gameObject, using Instantiate(gameObject) should make a second copy of this gameObject as it currently is so you should remove Instantiate(HexTile). It would also be a good idea to assign a new material to your renderer. However neither of these should make your gameObject invisible so I think the problem might be in another part of your code. Here is some code that works, try using it as a starting off point and see why your code does not create a visible object.

 GameObject gameObject=new GameObject("Triangle");
 
 MeshFilter filter=gameObject.AddComponent<MeshFilter>();
 Mesh mesh=new Mesh();
 mesh.vertices=new Vector3[]{
     new Vector3(0, 0),
     new Vector3(0, 1),
     new Vector3(1, 0),
 };
 mesh.triangles=new int[] { 
     0, 1, 2, 
 };
 mesh.uv=new Vector2[mesh.vertices.Length];
 mesh.RecalculateNormals();
 mesh.Optimize();
 filter.mesh=mesh;
 
 MeshRenderer renderer=gameObject.AddComponent<MeshRenderer>();
 renderer.material=new Material(Shader.Find("Diffuse"));
 renderer.material.color=Color.green;
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
avatar image
0

Answer by ShockdeadGames · Mar 17, 2015 at 09:04 PM

Instead of coding this, you can just add a mesh renderer to the GameObject.

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
avatar image
0

Answer by CladInShadows · Mar 18, 2015 at 02:54 PM

I can't see any differences between your code and mine. This is certainly frustrating. Could it be the way I am calling the function? My full code is as below (you can ignore the second region):

 using UnityEngine;
 using System.Collections;
 
 public class HexManager : MonoBehaviour {
 
     #region HexInfo
     
     public float HexUnitLength;
     
     [HideInInspector] public Vector3[] HexVertices;
     [HideInInspector] public Vector2[] HexUV;
     [HideInInspector] public int[] HexTriangles;
     
     public Texture HexTexture;
 
     [HideInInspector] public GameObject HexTile;
     
     void HexMeshSetup() {
         
         float floorLevel = 0;
         float HexSizeMult = HexUnitLength / Mathf.Sqrt(3);
 
         HexVertices = new Vector3[] {
             /*0*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(3+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(3+0.5)/6)))),
             /*1*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(2+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(2+0.5)/6)))),
             /*2*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(1+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(1+0.5)/6)))),
             /*3*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(0+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(0+0.5)/6)))),
             /*4*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(5+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(5+0.5)/6)))),
             /*5*/new Vector3((HexSizeMult * Mathf.Cos((float)(2*Mathf.PI*(4+0.5)/6))), floorLevel, (HexSizeMult * Mathf.Sin((float)(2*Mathf.PI*(4+0.5)/6))))
         };
         
         HexTriangles = new int[] {
             1,5,0,
             1,4,5,
             1,2,4,
             2,3,4
         };
         
         HexUV = new Vector2[] {
             new Vector2(0,0.25f),
             new Vector2(0,0.75f),
             new Vector2(0.5f,1),
             new Vector2(1,0.75f),
             new Vector2(1,0.25f),
             new Vector2(0.5f,0),
         };
 
         HexTile = new GameObject("Single Hex Tile");
 
         HexTile.transform.position = Vector3.zero;
         HexTile.transform.rotation = Quaternion.identity;
         HexTile.transform.localScale = Vector3.one;
 
         MeshFilter HexMeshFilter = HexTile.AddComponent<MeshFilter>();
         MeshRenderer HexMeshRenderer = HexTile.AddComponent<MeshRenderer>();
         
         Mesh HexMesh = new Mesh();
         HexMesh.vertices = HexVertices;
         HexMesh.triangles = HexTriangles;
         HexMesh.uv = HexUV;
 
         HexMesh.RecalculateNormals();
         
         HexMeshFilter.mesh = HexMesh;
         HexMeshRenderer.material.mainTexture = HexTexture;
 
     }
     
     #endregion
 
     #region GridGeneration
 
     public int GridWidth;
     public int GridHeight;
 
     public Vector3 C aalcInitPos() {
     
     // Position of the first tile placed will be the upper-left corner of the grid.
 
         Vector3 InitPos;
         InitPos = new Vector3(-HexUnitLength * GridWidth / 2f + HexUnitLength / 2,
                               0,
                               HexUnitLength * GridHeight / 2f - HexUnitLength / 2);
         
         return InitPos;
 
     }
 
     public Vector3 CalcWorldCoord(Vector2 GridPos) {
     
         Vector3 InitPos = CalcInitPos();
 
         float RowOffset = 0;
         if (GridPos.y % 2 != 0)
             RowOffset = HexUnitLength / 2;
         
         float x =  InitPos.x + RowOffset + GridPos.x * HexUnitLength;
         float z = InitPos.z - GridPos.y * HexUnitLength * 0.75f;
         return new Vector3(x, 0, z);
 
     }
 
     void CreateGrid() {
     
     //HexGrid will be a game object which is the parent of all the individual hex tiles
     
         GameObject HexGrid = new GameObject("HexGrid");
         
         for (float y = 0; y < GridHeight; y++) {
         
             for (float x = 0; x < GridWidth; x++) {
             
                 Vector2 GridPos = new Vector2(x, y);
                 GameObject HexClone = (GameObject)Instantiate(HexTile);
                 HexClone.transform.position = CalcWorldCoord(GridPos);
                 HexClone.transform.parent = HexGrid.transform;
             }
         }
     }
 
     #endregion
 
     #region Run
 
     void start() {
 
         HexMeshSetup();
         CreateGrid();
 
     }
 
     #endregion
 
 }
 
Comment
Add comment · Show 2 · 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 maccabbe · Mar 18, 2015 at 05:00 PM 0
Share

C# is case sensitive, on line 125 start() needs to be Start() if you want the $$anonymous$$onobehaviour to call it.

Also line 76 is a little messed up, should be public Vector3 CalcInitPos() {

I fixed these two errors and tested out your code (make sure to input values in the inspector).

avatar image CladInShadows · Mar 18, 2015 at 11:29 PM 0
Share

You are a genius! I knew it had to be something silly like that...

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Replace GameObject vs. replacing mesh and material? 2 Answers

Are Mesh Filters Really Necessary? 1 Answer

Game object with animations 0 Answers

Creating more objects with code. 2 Answers

Turning off renderer in other game object (C#) 2 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