Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Timurov · Nov 22, 2012 at 09:23 AM · c#gameobjectinstantiatearraylist

How to put gameObjects to the list?

I have gameObjects and I need to put them to the list, something like that:

 `List<GameObject> unityGameObjects = new List<GameObject>();`

Is it possible? If so, how to instantiate them though in another script?

Any answer will appreciated.

Comment
Add comment · Show 2
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 phodges · Nov 22, 2012 at 09:29 AM 0
Share

You can add GameObjects to a list in this way. If another script is responsible for instantiating them then they will need to do something like have a reference to this listing script and be able to call a method that informs the listing script to make a change.

avatar image AlucardJay · Nov 22, 2012 at 09:42 AM 0
Share

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

$$anonymous$$y list notes :

 // to use List
 import System.Collections.Generic;
 
 // declare
 var inRangeList : List.< GameObject > = new List.< GameObject >();
 
 // Add object to list :  
 inRangeList.Add( theItem );
 
 // length
 inRangeList.Count;
 
 // from position 
 inRangeList[ v ];
 
 // empty list
 inRangeList.Clear();
 
 // send to builtin array
 inRangeList.ToArray();
 

4 Replies

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

Answer by Timurov · Nov 22, 2012 at 11:56 AM

I'm answering my question: How to instantiate the list of game object in another script? Very easy, it is important to declare the list as public and use the name of the script where the list of game object is located:

 xmlreader.unityGameObjects.transform.localPosition = new Vector3(6,8,9);
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
3

Answer by dukearena · Nov 22, 2012 at 09:38 AM

Hi! The list you posted is correct, you can try this code for your question:

 // On Top ---
 include System.Collections.Generics;
 
 
 
 // Inside class --
 public GameObject prefab;
 List<GameObject> goList;
 
 
 void Start(){
   goList = new List<GameObject>();
 }
 
 void Update() {
   if(Input.GetKeyDown(KeyCode.I) {
     // This random position is for fun :D
     Vector3 rndPos = new Vector3(Random.Range(-20, 20), Random.Range(-20, 20), Random.Range(-20, 20));
     
     // Create a new GameObject from prefab and move to random position
     goList.Add((GameObject)Instanciate(prefab, rndPos, Quaternion.Identity);
   }
 
   if(Input.GetKeyDown(KeyCode.L)){
     Debug.Log(goList.Count);
     foreach(GameObject go in goList){
       Debug.Log(go.name);
     }
   }
 }

NOTE: I wrote "goList = new List();" in start because I like the good sintax programming, you should write "List goList = new List();" in declarations

Now, the complete example, modify Update and Start like following:

 OtherClass other;
 
 void Start(){
    other = GetComponent<OtherClass>();
 }
 
 void Update() {
       goList.Add(other.myGameObject);
     }

Now, create the OtherClass file:

 public GameObject myGameObject;
     
     
     public Start(){
       myGameObject = (GameObject)Instanciate(whateverPrefab);
     }


I hope your question is satisfied.. I'm sorry if I made mistakes, I wrote the code at the moment :D

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 RubikAdams · Nov 29, 2019 at 02:22 AM 0
Share

Hi! your good sintax program$$anonymous$$g helped me to another list issue about random audio into a List . Thanks @dukearena !

avatar image
0

Answer by jaxx0rr · Sep 18, 2013 at 06:47 AM

 function convertToList(tmpGol:GameObject[]):List.<GameObject>{
     var xz : int;
     var tmpList = new List.<GameObject>(); 
     for (xz=0;xz<tmpGol.Length;xz++){
         tmpList.Add(tmpGol[xz]);
     }
     return tmpList;
 }
 
 function searchAnotherRandomEnemy(){
     
     var unitPointList1 = new List.<GameObject>(); 
     var unitPointList2 = new List.<GameObject>(); 
     var unitPointList = new List.<GameObject>(); 
 
         if (searchTimer > 0) {
             searchTimer--;
             return;
         }
     searchTimer = 10;
 
     if (GNDturret) {
         unitPointList1 = convertToList(GameObject.FindGameObjectsWithTag("Enemy"));
         unitPointList.AddRange(unitPointList1);
     }
     if (AAturret) {
         unitPointList2 = convertToList(GameObject.FindGameObjectsWithTag("EnemyAir"));
         unitPointList.AddRange(unitPointList2);
     }
     print (unitPointList.Count);
     if (unitPointList.Count > 0){
         var randomO = Random.Range(0, unitPointList.Count);
         currentEnemy = unitPointList[randomO];
         EnemyScript = currentEnemy.GetComponent(AIEnemy);
     }
 }
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 davidnibi · Oct 11, 2019 at 11:19 PM

Just to clear the caps mistake in the above code:

          playerList.Add((GameObject) Instantiate (prefab, rndPos, Quaternion.identity));

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

15 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

Related Questions

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Can't Activate a GameObject from Array 1 Answer

How would I find the name of all game objects in a c# List 1 Answer

Store Game Object Into List For Later Reinstantiation? 0 Answers

Can´t instantiate objects in list correctly 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