• 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 /
  • Help Room /
avatar image
0
Question by HyTang · Sep 09, 2016 at 10:35 AM · c#classreference-other-object

Is the public class object in scripts only a reference of the game object's component?

I am new to both unity3D and C#. After creating my first "Hello world" program, I have a question about the public class object in script. In the level, I have two game objects (A & B). There are two scripts attached to these objects separately. The script A is as follows:

 public class ScriptA : MonoBehaviour
 {
 
     public int m_value1;
     public int m_value2;
     public int m_value3;
 
     // Use this for initialization
     void Start()
     {
         m_value1 = 0;
         m_value2 = 0;
         m_value3 = 0;
     }
 }

and the script B is:

 public class ScriptB : MonoBehaviour {
     public ScriptA m_objectA;
     public int m_value1 = 0;
     public int m_value2 = 0;
 }

I am now wondering that whether the m_objectA in scriptB only a reference of object A or the data of object A is stored in two places in my memory. I am really worried about this problem especially when I want to use object A to store enormous data. Thanks a lot!

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 tqkiettk10 · Sep 09, 2016 at 10:50 AM 0
Share

I think m_objectA just reference to object A. I believe that, becase if you have any sprite attach to A object and the B object call m_objectA.getComponent().sprite = anotherSprite; it will change on A (you can will on Game view).

avatar image AurimasBlazulionis · Mar 28, 2017 at 04:59 PM 0
Share

Hello, do you mind accepting my answer since it is correct. It really makes me stay motivated to answer these questions.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AurimasBlazulionis · Sep 09, 2016 at 11:10 AM

Classes stored in variables are only references. If you need different instances out of one, you will need to copy everything from the class. There are multiple solutions, one of them is deep copy. But do not do that with mono behaviours, unless you want to copy component to different object.

Comment
Add comment · Show 1 · 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 HyTang · Sep 09, 2016 at 02:35 PM 0
Share

Thanks a lot!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers

How to create a drop down menu in editor inspector 0 Answers

Use a class like a function 1 Answer

How to get the component in a new class 0 Answers

C# Find specific object by getting one of its variables 0 Answers


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