• 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 Quade81 · Jul 30, 2012 at 09:37 PM · c#arrays

How do I make an array of game objects in C#

I am trying to make a party based RPG style game. I want to store each unit in an array so I can scroll through the units, but the scripting reference doesn't allow me to switch from java to C# for the array entry and I'm not experienced enough with either language to be translating from one to the other. How would I make an array of game objects and access variables from the game objects through the array?

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

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

Answer by whydoidoit · Jul 30, 2012 at 10:05 PM

Well just to give a different perspective - I'd recommend using a List of GameObjects or a List of YourUnitType rather than an array. Lists are accessed using the same array semantics but you can add and remove items from them easily at runtime, unlike an array.

      public List<YourUnitType> units = new List<YourUnitType>();

      

      units.Add(someGameObject.GetComponent<YourUnitType>());
      units.Remove(someOtherGameObject.GetComponent<YourUnitType>());

      foreach(var unit in units)
      {
      }

      unit[0].transform.position = Vector3.zero;

      unit[0].yourVariableHere = whatever;
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 Quade81 · Aug 02, 2012 at 06:45 PM 0
Share

Thanks for the suggestion. It's just what I was looking for.

avatar image
1

Answer by ScroodgeM · Jul 30, 2012 at 09:50 PM

public GameObject[] MyObjects;
void Start()
{
    Instantiate(MyObjects[3]); // instantiates 4th object
}
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 Mortoc · Jul 30, 2012 at 09:51 PM 0
Share

Scroooodge! Way to get your answer up 30 seconds before me.

avatar image ScroodgeM · Jul 30, 2012 at 09:58 PM 0
Share

gold is $$anonymous$$e. silver is yours... who wants bronze? 8)

avatar image
0

Answer by Mortoc · Jul 30, 2012 at 09:50 PM

It would look something like this. You could put this script on a GameObject and in the editor set up as many units in that array as you want. I do recommend using better names than I did:

 public class ObjectThatContainsUnits : MonoBehaviour
 {
     public GameObject[] _anArrayOfUnits;
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers

Obtaining the Variable of a Prefab in an array. 1 Answer

How do i convert this code snippet to a list? 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