Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by omg_Aya · Nov 05, 2014 at 04:11 PM · arraymeshrenderercopy

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 [i].material.shader = tempShader;
                     tempArray[i].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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omg_Aya · Nov 05, 2014 at 05:40 PM 0
Share

Also doesn't work using $$anonymous$$aterial or Shader

avatar image Kiwasi · Nov 05, 2014 at 05:50 PM 0
Share

You are getting a new array. However $$anonymous$$eshRenderes, are passed by reference. So you get a reference to the same $$anonymous$$eshRenderer.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by NoseKills · Nov 05, 2014 at 06:37 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omg_Aya · Nov 05, 2014 at 07:07 PM 0
Share

Well copying all of the entire gameobjects would be even worse. If thats really what I have to do to get a copy of just this one piece I want that's kinda terrible. I just want the Shader. I have another way I can do it but this is just annoying.

avatar image Kiwasi · Nov 05, 2014 at 10:33 PM 1
Share

The way to solve this would be to make simply store the variables you plan to change. Then set those back to the original when you are done

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to copy part of an array 2 Answers

Array.Copy in UnityScript 1 Answer

Problem with copying array that holds Raycasthit objects 1 Answer

How do I Resize an Array in JS? 1 Answer

How to copy a multidimensional array? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges