Array.Copy still creating a reference.

Hey all,

So I am trying to create a copy of an array so that I can revert back to the original array later. But for some reason I can’t seem to make a copy and always get a reference.

Here is my code:

  MeshRenderer[] tempArray = AffectedObject.GetComponentsInChildren<MeshRenderer> ();
    			m_PrevShaders=new MeshRenderer[tempArray.Length];
    			Array.Copy(tempArray,m_PrevShaders,tempArray.Length);
    
    			if (tempArray.Length>0)
    			for (int i=0; i<tempArray.Length; ++i) {
    				Shader tempShader=Shader.Find(m_ShaderName);
    				tempArray *.material.shader = tempShader;*

tempArray*.material.SetColor("OutlineColor",m_Color);
_
}*

Any idea why it’s not working? I think it might be because I’m copying a MeshRenderer or something but am not sure.
Thanks

Like @BoredMoron said, if you have an array of 10 instances of MeshRenderer (or whatever class), if you Array.Copy the array, you still have the same 10 MeshRenderers, but now you have 2 arrays referencing them.

You might think “Why can’t i just make temporary backups like this? Why did they make this so difficult”.

If you look at what inherited members MeshRenderer has, it’s easy to see why that wouldn’t actually make things any easier.

Imagine you’d do duplicates of your MeshRenderers. OK, now in the new MeshRenderers, does meshRenderer.gameObject refer to the same GameObject as in the original one or should we then duplicate the associated GameObjects too? What about attached colliders ? Transforms ? etc…

Depending on how you’d answer the questions above, by making a backup of a small set of MeshRenderers, you’d be either A) making also a huge bunch of other objects easily wasting tons of memory, or B) ending up with the same problem as you have: the copies are not entirely new copies but instead referencing some of your old stuff.