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 MightyLampshade · Mar 21, 2013 at 05:32 PM · errorinstantiatechildloop

Instantiating children in loop not working

Hello, I have tried to instantiate these prefabs in a loop as children of an object that already exists in the scene called "LevelLoad". For some reason, it only renders a single object instead of the several hundred I'm expecting.

These objects are created by using a 2D map (which I've already implemented and it works great) to make a "tiled" area.

I honestly have no idea why it isn't working. When I run it without trying to parent any of the objects they all appear correctly, it seems to be something to do with my parenting. It comes up with this error on the line:

     go.transform.parent = parentGameObject.transform;

"NullReferenceException: Object reference not set to an instance of an object"

Here is my code, please take a look. If you need more details, please say so :D

     void Start () {
         //parentGameObject = new GameObject();
         Debug.Log ("Locate parent object");
         parentGameObject = GameObject.Find("LevelLoad");
         Debug.Log ("Parent object located. Name: " + parentGameObject.name);

And here is the section from the LoadROSMap() method

         // load map from path
         if(map != null)
         {
             // create bitmap from image
             Debug.Log ("Creating bitmap from map iamge");
              Bitmap rosMap = new Bitmap(map);
             Debug.Log ("Bitmap Props: x: " + rosMap.Width + " y: " + rosMap.Height);
         
             
             
             // lets try the getpixel method
             for(int y =0 ; y < rosMap.Height; y++)
             {
                 for(int x = 0; x < rosMap.Width; x++)
                 {
                      if(rosMap.GetPixel(x, y) == black)
                     {            
                         
                         GameObject go = Instantiate(wallObject, new Vector3(x,0,y), Quaternion.identity) as GameObject;
                         
                         if(parentGameObject)
                             go.transform.parent = parentGameObject.transform;
                         Debug.Log("Instantiating wall object: " + x + ", " + y);
                         
                     }
                         // draw me a cube at these co-ords
                         //if(rosMap.GetPixel(x, y) == white)
                     //{
                         //Instantiate(floorObject, new Vector3(x,0,y), Quaternion.identity);
                     //}
                             // draw me a plane at these co-ords
                             //if(rosMap.GetPixel(x, y) == black)
                                 // do nothing
                 }
             }
         }
         else
             Debug.Log("Failed to read PGM image");
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 whydoidoit · Mar 21, 2013 at 05:34 PM 0
Share

How have you declared wallObject? What type of variable is it?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Mar 21, 2013 at 05:38 PM

So one of two things is happening:

(1) You have declared the variable wallObject as something other than GameObject.

Instantiate makes an object of the type of variable that the prefab is stored in. For example if the wallObject is stored in a Transform variable then you must do:

  Transform t = Instantiate(wallObject, new Vector3(x,0,y), Quaternion.identity) as Transform;

(2) The game object "LevelLoad" was not found for some reason (incorrect spelling, object disabled etc).

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 MightyLampshade · Mar 22, 2013 at 02:58 PM

Step 1 worked perfectly! I don't know how I didn't see that before, I had tried to use Transform before but I don't think I had put the identifier "as Transform" on the end.

Thankyou so much :D

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

11 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

Related Questions

Getting instance of an sub object rather than the original's subobject 0 Answers

Cannot cast from source type to destination type. 1 Answer

Renaming Child Objects during Instantiate? 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

Simultaneous Null Reference Exception and expected value 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