- Home /
How to copy part of an array
I have an array in a GameObject 'Core', I also have an array in GameObject 'Player', both arrays are class arrays 'Monsters[]'but when i use System.Array.Copy(Core.monsters, monsters, 151) the array is still referencing the array in Core. Here is the code inside Player.
public class PlayerMonsters : MonoBehaviour {
public Monsters[] monsters = new Monsters[151];
public Monsters[] team = new Monsters[6];
public GameObject CoreObj;
void Start()
{
Core Core = CoreObj.GetComponent<Core>();
System.Array.Copy(Core.monsters, monsters, 151);
team[0] = monsters[5];
}
}
All i want to do is create a array of six monsters from Core.monsters without altering the values of Core.monsters.
Doing this is start was a bad idea as it doesn't finish System.Array.Copy before $$anonymous$$m[0] = monsters[5] is executed.
It's not clear what the issue is.
You definitely have 2 array variables called monsters, one in Core and one in Player$$anonymous$$onsters, you are copying elements from the former into the latter. And this shouldn't change the value of, or contents of, Core.monsters.
But it won't do a deep copy. The values in these arrays are pointers to $$anonymous$$onsters and the $$anonymous$$onsters objects being pointed to by the values in Player$$anonymous$$onsters.monsters and thence in Player$$anonymous$$onsters.$$anonymous$$m are the exact same ones in Core.monsters. So if you change a $$anonymous$$onster via either of the arrays in Player$$anonymous$$onsters you are changing the ones pointed to by Core.monsters.
If you want to do a deep copy you need to copy each $$anonymous$$onster, not just the pointers.
So for example if you want your $$anonymous$$m array to have new $$anonymous$$onsters that are copies of what's in the longer list you could create a copy constructor and change the last line to $$anonymous$$m[0] = new $$anonymous$$onster(monsters[5])
and so on. Same for the monsters array.
BTW I'm not sure what Raymond is getting at. The array copy will finish before the next line is executed.
Bonfire boy, When place $$anonymous$$m[0] = monsters[5]; in a method and invoke it from start with a slight delay the problem goes away, whereas leaving it in start results in monsters[5] having all empty values. I will try $$anonymous$$m[0] = new $$anonymous$$onster(monster[6]) tonight thank you for the suggestion
This is because your $$anonymous$$onster object is created after the start method is run (I don't see where and when though).
$$anonymous$$m[0] = monsters[5];
when you run that too early, $$anonymous$$m[0] gets a null value as monsters[5] is null then later monsters[5] gets the object but $$anonymous$$m[0] is not pointing there as it does not get updated.
Copy is done at once so the next line is done once it is finsihed. Your issue here seems to be that declaring an array of 151 monsters does not create 151 monsters. It only set 151 references in memory waiting to receive the addresses of 151 monster objects you have yet to create.
Answer by HarshadK · May 26, 2015 at 11:19 AM
If you look at the MSDN documentation for Array.Copy method, it states that:
If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. A shallow copy of an Array is a new Array containing references to the same elements as the original Array.
So as you are experiencing in your case it is performing a shallow copy.
If you want to create a deep copy of the elements then you can implement the IClonable Interface on your Core class so that monsters array can be deep copied. Although remember there are certain downsides to using the IClonable interface as discussed on the same page which states:
The ICloneable interface simply requires that your implementation of the Clone method return a copy of the current object instance. It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. Nor does it require all property values of the original instance to be copied to the new instance. For example, the NumberFormatInfo.Clone method performs a shallow copy of all properties except the NumberFormatInfo.IsReadOnly property; it always sets this property value to false in the cloned object. Because callers of Clone cannot depend on the method performing a predictable cloning operation, we recommend that ICloneable not be implemented in public APIs.
Thank you, i read the documentation but it was really late. This makes perfect sense i understand and should easily be able correct the problem
Answer by HTwist · May 26, 2015 at 10:56 AM
If i am not wrong, it will copy the array but both arrays will still have references to the same set of monsters.
So by modifying a monster you will modify it in both arrays.
This is correct but i am unsure on how to get around the issue, it has been bugging me for a while now
Your answer
Follow this Question
Related Questions
Alter a GameObjects(Clone) array from another script C# 0 Answers
Find GameObjects with a true boolean and put them in an array? 1 Answer
2D array of GameObjects C# 2 Answers
Multiple Cars not working 1 Answer
Using getcomponent with an array 2 Answers