Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by JoelAtMoodkie · Apr 22, 2010 at 01:04 PM · instancecopypointer

Will this be an instance or a pointer?

Hiya all,

I'm just going to use a rough example here in pseudo code. Lets say that in Javascript I have an ObjectManager class:

class ObjectManager { // A singleton object private var anObject : MyObject = new MyObject();

// Accessor method static function getAnObject() : MyObject { return anObject; } }

Now lets say I have a class (randomClass):

class randomClass { private var randomObject : MyObject;

function Start() { anObject = ObjectManager.getAnObject(); } }

The Question: Is the variable 'randomObject' a pointer to 'anObject', or is it a copy of 'anObject'?

Comment
Add comment · Show 1
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 headkit · Apr 22, 2010 at 01:18 PM 0
Share

interested I am!

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ray-Pendergraph · Apr 22, 2010 at 01:32 PM

Yes that is a simple implementation of singleton. As long as the instance of anObject is never replaced in ObjectManager it will continue to hand out that particular instance. Anything you do to the instance (any state changes) outside of the ObjectManager class will in fact be done to the private instance in the ObjectManager class. They are the same. There are no pointers in JS (the term of art is reference here), but I think that is what you mean.

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 Ray-Pendergraph · Apr 22, 2010 at 01:37 PM 0
Share

Also note that you can deter$$anonymous$$e if two references point to the same instance on the heap with the == operator.

refOne = Object$$anonymous$$anager.getAnObject(); refTwo = Object$$anonymous$$anager.getAnObject();

Then refOne == refTwo should be true.

The story is a little different for strings in .NET but for this it's true.

avatar image JoelAtMoodkie · Apr 22, 2010 at 03:15 PM 0
Share

Thank-you so much ... that is exactly what I needed (and wanted) to know ;)

avatar image
1
Best Answer

Answer by Lucas Meijer 1 · Apr 22, 2010 at 01:33 PM

It's a reference to anObject. You can have multiple references, all referencing a single 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 JoelAtMoodkie · Apr 22, 2010 at 03:16 PM 0
Share

Thanks for the answer!

avatar image
2

Answer by Molix · Apr 22, 2010 at 03:11 PM

Important: if you want it to be a singleton you'll need to declare the variable as static. Otherwise each instance of ObjectManager will have its own anObject, which is exactly the opposite of having a single one. (Also, ObjectManager won't compile unless the variable is static because it is being referred to in the static function getAnObject().

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Instantiate an instance (clone) only copies public fields? 1 Answer

Copy / Paste As New not copying deep 1 Answer

Moving and manipulating object trought scenes in Unity 4 0 Answers

copy read only animation? 1 Answer

Creating Prefab causes scripts to break, Copy/Paste Gameobject doesn't 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