If I change a tempArray initialized from the original, does it affect the original?

var waypoints : Array;

waypoints = GameObject.FindGameObjectsWithTag(“waypoint”);

var tempWPArray : Array = waypoints;

In my code I’ve made the waypoints array and initialized it so that’s fine but when I access it later, it hits me with an indexoutofboundserror even though I haven’t added or taken anything away from the waypoints array. But one thing I did was create a copy and remove things from that array. So I’m wondering, if I make changes to the tempWPArray, will it carry over to the waypoints array?

Here's my "It's sometimes better to lie" version of the answer: arrays have been removed from computing, you must use List now. ;-)

1 Answer

1

Arrays are by reference. So your tempWPArray is not a copy, it’s just a pointer to the waypoints array. (By the way, there’s no reason to use the Array class, and a number of reasons to avoid it. Use a generic List instead. Also that way you can use the MemberwiseClone function to make an actual copy of the list.)