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
1
Question by tobobox · Aug 14, 2012 at 05:53 AM · c#gameobjectinstantiate

C# instantiate into list

Why am I having trouble instantiating into a list? When I my script I get the following error: NullReferenceException: Object reference not set to an instance of an object. I'm using a public variable that has a game object placed in the inspector. I don't understand why it shows as null.

 public GameObject _gameObject;   
 List <GameObject> _list; 
  
 //--------------------------------  
 void Start(){ 
       for(int i=0; i<5; i++){
             _list.Add( (GameObject)Instantiate(_gameObject) );
       } 
 }
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

2 Replies

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

Answer by Meltdown · Aug 14, 2012 at 06:19 AM

You haven't initialised your list yet..

 void Start()
 { 
       _list = new List<GameObject>();
     
       for(int i=0; i<5; i++){
             _list.Add( (GameObject)Instantiate(_gameObject));
       } 
 }
Comment
Add comment · Show 3 · 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 Numid · Aug 14, 2012 at 07:12 AM 0
Share

Cast operation is already perform prefixing "Instantiate" with "(GameObject)". Thus "as GameObject" isn't needed.

avatar image Meltdown · Aug 14, 2012 at 07:13 AM 0
Share

Oops I didn't even see that. I'm so used to doing it the other way lol

avatar image tobobox · Aug 14, 2012 at 01:32 PM 0
Share

initializing my list solved the problem. Speedything is also correct that you need to use "using System.Collections.Generic;". You don't need to pass it through a temp gameobject before adding to the list though.

Thanks guys!

avatar image
1

Answer by speedything · Aug 14, 2012 at 10:28 AM

Have you added the this line to the top of your code?

 using System.Collections.Generic;

If you have then try changing your start code to this,

 void Start() {
 List<GameObject> _list = new List<GameObject>();
     for(int i=0, i<5, i++) {
        GameObject thisObject = Instantiate(_gameObject) as GameObject;
        _list.Add(thisObject);
     }
 }

I'm not entirely sure what the difference is, but that code is in the format I use and always works fine.

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 Meltdown · Aug 14, 2012 at 10:30 AM 0
Share

That won't work as you haven't initialised your list. And you won't get a null reference exception for missing a namespace declaration

avatar image speedything · Aug 14, 2012 at 01:21 PM 0
Share

Sorry, I left that bit of code out... Edited the answer

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

10 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

Related Questions

gameObject are not referenced 2 Answers

Rotating a Vector3 in Instantiation 1 Answer

How do I make prefab follow a gameobject?(C#) 1 Answer

AI Instantiate targets/prefab. (pick target issue) 1 Answer

References to GameObject become null 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