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
0
Question by Freesmith · Feb 05, 2013 at 03:35 PM · c#instantiatefunctionreturn

C# Function that returns instantiated object

Hello, I need kind of help..

Here I'm trying to make a function that will return instantiated object, and put it into variable. here is the code sample;

    public CLine CreateLine(Vector3 LinePos, CLine LinePrefab)
 {
     CLine NewLineScript;
     NewLineScript =  Instantiate(LinePrefab, LinePos, Quaternion.identity) as CLine;
     Return (NewLineScript);
 }

   CLine LineScript;
   LineScript = CreateLine(LinePos,LinePrefab);


The question is ,

Does the function returns instantiated object or the pointer to him?

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 Landern · Feb 05, 2013 at 03:51 PM 0
Share

Is the code above actually how you have it in your class? i.e. you are calling a method to return an instantiated CLine on a field?

avatar image Wolfram · Feb 05, 2013 at 04:53 PM 0
Share

It returns a "pointer", although it isn't called one and doesn't look like one ;-) As the others have noted, it's a reference.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Dave-Carlile · Feb 05, 2013 at 03:44 PM

In C#/.NET, any variable that "contains" an object instance in reality contains a reference to the object instance. When passing the variable around (either as a parameter, or returning it from a function) you're passing around the reference, not the object data itself.

This is completely transparent to you as a developer - it has no affect on the syntax you use to access the object. So the question is, why does this matter to you? It's rare that it should.

Comment
Add comment · Show 5 · 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 Freesmith · Feb 05, 2013 at 04:19 PM 0
Share

Well, since in C++ it is easy to distinguish pointers from regular variables, here I'm not sure whether the whole object is copied in return function or just a reference (pointer) is passed through.

Would it be any different if it would look like this?

 public CLine CreateLine(Vector3 LinePos, CLine LinePrefab)
 {
     CLine NewLineScript;
     NewLineScript =  new CLine();
     // Do something
     Return (NewLineScript);
 }
avatar image zombience · Feb 05, 2013 at 04:23 PM 1
Share

correct. c# handles pointers and memory management for you. the only time you have a duplicate variable is if you explicitly create a new variable.

assignment operators in c# pass references to objects. it's the a great beauty of c#, but could also be frustrating if you want to get into lower level memory management

avatar image Dave-Carlile · Feb 05, 2013 at 04:39 PM 0
Share

As @zombience said, .NET handles everything as references - unless you specifically clone an object you're always dealing with the reference. Instantiate creates a clone of the object, so maybe that's the question you're getting at?

avatar image Wolfram · Feb 05, 2013 at 04:51 PM 2
Share

Just for reference (pun intended): Note this is true for everything that's a class. However, stuff like Vector3 and Color and so on are not classes (essentially, they are "struct"s), so they will be passed by value, not as a reference. See also http://msdn.microsoft.com/en-us/library/s1ax56ch(v=vs.80).aspx

avatar image Freesmith · Feb 05, 2013 at 05:11 PM 0
Share

Things are much clearer now, Thanx all.

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

13 People are following this question.

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

Related Questions

How can I call a function in all instances of a script 3 Answers

Distribute terrain in zones 3 Answers

C# Return Type Error? 1 Answer

Why does this prefab trigger all instances to action? C# 1 Answer

Multiple Cars not working 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