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 gretty · Jul 13, 2012 at 06:55 AM · gameobjectdynamicinstantiation

Create Special Plane Objects at runtime

I am attempting to create 5 Special Plane objects on the fly at runtime.

My Problem: The objects(SpecialPlane) are created but they are never shown or added to the 'Hierarchy'. I have an empty game object called TerrainManager, it has a C# script added to it that is supposed to create 5 SpecialPlane objects/MonoBehaviour objects then add them to the hierarchy. It successfully creates the 5 planes AFAIK but it doesn't add them to the hierarchy, why?

How can I successfully create 5 SpecialPlane objects (dynamically at runtime) and add them to the hierarchy?

 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TerrainManager : MonoBehaviour {
  
  static readonly uint DEF_PLANE_NUM = 5;
  List <SpecialPlane> planes;
  
  void Start () {
  
  // Create n planes: this code compiles and runs but the planes are never shown?
  for (int i=0; i<DEF_PLANE_NUM; i++) {
  SpecialPlane plane = gameObject.AddComponent<SpecialPlane>();
  planes.Add (planes);
  }
  
  // read file that contains a list of url's that lead to a .png which is a texture
  // for each plane: get them to download their mesh from the url obtained from the text file
  // setPlaneMesh("www.url.com/mesh/1.png");
  }
  
 }
 
 public class SpecialPlane : MonoBehaviour {
  
  void Start () {
  
  MeshRenderer r  = gameObject.AddComponent<MeshRenderer>();
  MeshFilter f    = gameObject.AddComponent<MeshFilter>();
  }
  
  void setPlaneMesh(string urlMesh) {
  StartCoroutine( downloadMesh(urlMesh) );
  }
 }
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 whydoidoit · Jul 13, 2012 at 07:18 AM

You code would not add them to the hierarchy, but add them as scripts on the gameObject that has TerrainManager attached to it - was that your intention? If you want them as individual objects then you would need to create a GameObject inside the initialization loop - you also appear to have a typo where you add them to the List - I'm surprised this compiles...

  const int DEF_PLANE_NUM = 5; //Should be consistent between int and loop counter
  List <SpecialPlane> planes;

  void Start () {

        // Create n planes: this code compiles and runs but the planes are never shown?
        for (var i=0; i<DEF_PLANE_NUM; i++) {
            var go = new GameObject("SpecialPlane");
            var plane = go.AddComponent<SpecialPlane>();
            planes.Add (plane); //You had planes here in your example
        }
  }
  ...
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Bounds is 0 when Instantiated 2 Answers

Emptying and filling Arrays on command? C# 1 Answer

How to select a gameobject by script dynamically 1 Answer

Fastest way to instantiate thousands of objects at runtime? 1 Answer

dymanic create object while character approaches 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