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 stereosound · May 30, 2013 at 08:11 PM · c#arraysettersgetters

Getters/Setters with C# Array

Hi all, in an attempt to condense code I am trying to keep an array of GetComponents for an array of gameobjects that are updated every time the array of gameobjects is updated with getters/setters. Here's how it would work in my head:

 private GameObject[] _SomeGO = new _SomeGO[3];
 public SomeScript[] SomeGOScripts = new SomeGOScripts[3];
 
 public GameObject[] SomeGO [int location] {
    get {return _SomeGO[location]};
    set {
       _SomeGO[location] = value;
       if (_SomeGO[location] == null) {
          SomeGOScripts[location] = null;
       }
       else {
          SomeGOScripts[location] = value.GetComponent<SomeScript>() as SomeScript;
       }
    }
 }

However, this errors tremendously (I don't think SomeGO is done correctly, but I don't know how else to capture 'location'). Any ideas on how to properly use getters/setters with a C# 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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by saschandroid · May 30, 2013 at 08:51 PM

I hope this helps:

 public GameObject[] mSomeGO = new GameObject[3];

 public GameObject[] SOME_GO
 {
    get
    {
       return mSomeGO;
    }
    set
    {
       mSomeGO = value;
    }
 }

There is no need to use something like 'location' in the getter/setter - just use it like

 GameObject newGameObject = SOME_GO[2]

after filling the array (for example in the inspector) and it will work.

Comment
Add comment · Show 12 · 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 stereosound · May 30, 2013 at 11:19 PM 0
Share

This was very helpful! is there any if I do like SO$$anonymous$$E_GO[3] = aGameObject; that I can access the '3' in the setter?

avatar image saschandroid · May 31, 2013 at 08:07 AM 1
Share

Yes, that should work ... but make sure you initialized your array with the right size/big enough (or fill it within the inspector).

avatar image saschandroid · May 31, 2013 at 12:09 PM 1
Share

Here is exactly what I did in two classes:

 public class Whatever : $$anonymous$$onoBehaviour
 {
    public GameObject[] someObjects;
    void Start()
    {
       CONFIG.GA$$anonymous$$E_OBJECTS  = someObjects;
    }
 }
 
 public static class CONFIG
 {
    private static GameObject[] mGameObject;
 
    public static GameObject[] GA$$anonymous$$E_OBJECTS
    {
       get
       { 
          return mGameObjects;
       }
       set
       {
          mGameObjects = value;
       }
    }
 }

I attached the class Whatever onto an empty GameObject and filled the array within the inspector. On Start() I saved the GameObjects in a static class called CONFIG. Now I can access these GameObjects and their components from all my classes by calling CONFIG.GA$$anonymous$$E_OBJECTS[someNumber]. I really don't know why it doesn't work for you, sorry.

avatar image saschandroid · May 31, 2013 at 01:50 PM 1
Share

With

 mGameObjectScript = GameObjects[location].GetComponent<SomeScript>();

your are exchanging SomeScript with SomeScript. Why do you wanna do that when they are the same? If they are different scripts, you can't simply set ScriptA = ScriptB afaik. You have to remove/add or disable/enable them on your GameObject.

avatar image saschandroid · May 31, 2013 at 03:00 PM 1
Share

If you save a GameObject to GA$$anonymous$$E_OBJECTS (in my example) with

 CONFIG.GA$$anonymous$$E_OBJECTS[0] = singleGameObject;

doesn't it store every script and current value that's on singleGameObject with it?

Show more comments

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

Make a public variable with a private setter appear in inspector 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Getter / Setter and Variable Scope Question? 1 Answer

Get and Set difference 2 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