- Home /
How to pass objects through array?
So here is how the code looks like:
SomeClass someObject1;
GameObject gamObjX;
SomeClass[] myArray;
void Start ()
{
myArray = new SomeClass[1];
SomeMethod(ref gamObjX);
}
void SetElements ()
{
myArray[0] = someObject1;
}
void SomeMethod (ref GameObject gamObj)
{
myArray[0] = gamObj.GetComponent<SomeClass>(); // SomeClass script is attached to gamObj
}
void Update ()
{
someObject1.DoSomethingWithGamObj() // Error: Object reference not set to an instance of an object
myArray[0].DoSomethingWithGamObj() // Something is done
}
The only way I know how to solve this is using pointers. But Unity doesn't allow pointers so... Why does it happen? Are the elements in the array copies of actual variables? What happens when I call SetElements() method? I would like myArray[0] to be someObject1 (or 'reference of' should I say?) so if I do myArray[0].someVar = 1; print(someObject1.someVar);
would print 1 instead of null or some basic value.
Answer by perchik · Sep 27, 2013 at 06:19 PM
A few things:
First, it's not that Unity does't allow pointers, its that C# doesn't have pointers. [If you think you're writing Javascript(UnityScript) you're doing it way wrong]
Second, since gamObj
is global, you don't have to pass it by reference (or pass it in at all)
Now, to answer your question, I think you're confused at how things work.
Just because you set myArray[0] = someObject1, doesn't mean that someObject1 will always equal myArray[0].
You're correct in thinking if you had used pointers, this might work. Anyway, to fix it:
After you do myArray[0] = gamObj.GetComponent(); // SomeClass script is attached to gamObj
just do someObject1 = myArray[0];// or gameObj.GetComponent();
to actually assign the component to someObject1
Now. After that's all said, I'm not sure why you want two variables that reference the same component.
Works like a charm, thank you very much. Is there any way to skip the assigning step, so by operating on myArray[0] I would operate on someObject1 at the same time? I want it so because array can be used in loops, and standalone variable gives me clear view at whats happening in the code.
If my answer solved your original question, please mark it as the answer.
Now, with that said, I'd say if you really really want that functionality, switch to using a dictionary with the [string] name of the object as the key, and the component as the value. Then you could loop over it using the dictionary, or access specific items clearly with the dictionary... something like this:
using System.Collections.Generic;
Dictionary<string, SomeClass> myDict;
void Start(){
myDict = new Dictionary<string, SomeClass>();
myDict.Add("someObject1", gamObj.GetComponent<SomeClass>());
//or
myDict["someObject1"] = gamObj.GetComponent<SomeClass>();
}
void heresHowToLoopOver(){
foreach( $$anonymous$$eyValuePair<string, SomeClass> pair in $$anonymous$$yDict){
Debug.Log("Doing something with object: " + pair.$$anonymous$$ey);
pair.Value.DoSomethingWithGamObj();
}
//or loop over the keys:
foreach( string key in $$anonymous$$yDict.$$anonymous$$eys){
Debug.Log("Doing something with object: " + key);
myDict[key].DoSomethingWithGamObj();
}
}
void heresHowToAccess(){
myDict["someObject1").DoSomethingWithGamObj();/// there's your code clarity.
}
Btw, this isn't guaranteed to compile, I just wrote it on the fly