Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Aug 15, 2016 at 05:43 PM by Smurfj3 for the following reason:

Fixed it by doing a workaround

avatar image
1
Question by Smurfj3 · Aug 14, 2016 at 09:31 PM · gameobjectinstantiatelist

Instantiating GameObject and Adding to list

So like the title says I am trying to instantiate an GameObject and add it to a list, in this case it's to manage the gameobjects because I do not want to many in my scene. Code:

 void Start () {
         for(int x = 0; x < MaxAmountWolfs; x++)
         {
             GameObject wolf= Instantiate(Wolfs);
             Spawn.Random(wolf); // this is just another class that I use to give the object a random spawn location within the navmesh boundaries.

             lstWolfs.Add(new AIAnimal { Animal = wolf});
         }  
         Debug.Log(lstWolfs.Count);
         foreach(AIAnimal animal in lstWolfs)
         {
              Debug.Log(animal.name);
         }        
 }

The objects spawn just fine and they appear in the hierarchy which is good. however when I debug the list all the objects are null... which makes absolutely no sense to me.

The class AIAnimal is simply:

 public class AIAnimal : MonoBehaviour {
     public GameObject Animal { get; set; }
 }

I probably making some stupid mistake, I just don't see it.

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

3 Replies

  • Sort: 
avatar image
0

Answer by TheDawningLegend · Aug 14, 2016 at 09:59 PM

This should work :

   void Start () {
       for(int x = 0; x < MaxAmountWolfs; x++) {
           GameObject wolf = Instantiate(Wolfs) as GameObject;  //Instantiante the wolf.
           lstWolfs.Add(wolf); //Add it to your list
           Spawn.Random(wolf); //Spawn it somewhere
           new AIAnimal { Animal = wolf} //Set the AI
       }
   }

Add the instantiated GameObject instead of the AI.

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 Smurfj3 · Aug 15, 2016 at 12:10 AM

I havent tried that but that makes no sense, AIAnimal contains just set and get properties, and as soon as I spawn it it's not null so why would it become null when I add it to the list. also the list is declared as :

 List<AIAnimal> lstwolfs = new List<AIAnimal>();

So it expects an AIAnimal class, not just a gameobject... so adding wolf won't work :)

Comment
Add comment · Show 1 · 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 TheDawningLegend · Aug 15, 2016 at 01:50 AM 0
Share

What if the list was set for GameObjects? You could still access the your wolves in the list.

avatar image
0

Answer by DiegoSLTS · Aug 15, 2016 at 02:26 AM

Using "new" for components is not recommended, you have to use GameObject.AddComponent method.

Something like this...

 for(int x = 0; x < MaxAmountWolfs; x++)
 {
     GameObject wolf= Instantiate(Wolfs);
     Spawn.Random(wolf);
     AIAnimal aiComponent = wolf.AddComponent<AIAnimal>();
     aiComponent.Animal = wolf;
     lstWolfs.Add(aiComponent);
 }

Also, that "Animal" variable inside the AIAnimal class looks redundant... why not just use gameObject? Every MonoBehaviour has the "gameObject" field that's a reference to the GameObject it's attached.

Comment
Add comment · Show 1 · 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 Smurfj3 · Aug 15, 2016 at 05:42 PM 0
Share

Yeah that's how I temporarely fixed it myself just by making the list of type GameObject, however I was gonna add more properties to the AIAnimal but I will just do a workaround...

By the way the weird thing is that it did work before. It did return null for every item AIAnimal in the list but after a certain amount of frames they were not null anymore. I think it may have to do with the script execution order.

Follow this Question

Answers Answers and Comments

72 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 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 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 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

Changing GameObject values from a list 0 Answers

Check if a large number of gameobjects are colliding 1 Answer

Instantiating gameObjects, adding them to a list then moving every gameObject in the list.,Moving gameobjects in gameobject list 0 Answers

ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer

Gameobject list does not retrieve the first index in for loop 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