- Home /
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?
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.
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?
Yes, that should work ... but make sure you initialized your array with the right size/big enough (or fill it within the inspector).
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.
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.
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?
Your answer

Follow this Question
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