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 /
  • Help Room /
avatar image
1
Question by GarthLaoshi · May 13, 2016 at 08:58 AM · newreferences

Does Vector3 B = new Vector3(X,Y,Z) + A create B as a reference to A?

In other words, if I use the new keyword to declare a variable, does it automatically apply to the entire line or just the following term? How do I know if I'm creating a reference or not?

(assume, obviously, that A is also a vector 3 or other object that can be cast to vector 3).

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

1 Reply

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

Answer by Naphier · May 13, 2016 at 09:11 AM

"new" only applies to the constructor following it. Vector3 is a struct and thus is a bad example for reference because it is always a copy.

So

 Vector3 a = new Vector3(1,2,3);
 Vector3 b = a;
 b.x = 0;

Will still result in a.x = 1 If you just say

 Vector3 c;

Then that results in the default value of (0,0,0). c is a new Vector3.

If it were not a struct then it would be a reference. For example, say you have a class Foo.

 Foo a = new Foo();
 Foo b = a;
 b.field = 1;

a.field will also = 1. b is a reference to a.

This may provide further explanation: http://www.albahari.com/valuevsreftypes.aspx

Comment
Add comment · Show 4 · 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 GarthLaoshi · May 13, 2016 at 03:12 PM 0
Share

If

Vector3 is a struct and thus is a bad example for reference because it is always a copy

Then why do I need to use the "new" keyword at all when using Vector3s?

avatar image Owen-Reynolds GarthLaoshi · May 13, 2016 at 03:23 PM 0
Share

Look this up on C# forums. Lots more info about how/why C# does things a certain way. I think I even wrote about that exact thing on StackOverflow.

avatar image Naphier GarthLaoshi · May 13, 2016 at 04:24 PM 0
Share

You don't *have*to. You could also do Vector3 a; a.x = 1; a.y = 2; a.z = 3; The struct constructor is provided because it is convenient. $$anonymous$$ost structs will have constructors. All constructors are called with new.

avatar image Bunny83 GarthLaoshi · May 14, 2016 at 01:17 AM 0
Share

The "new" keyword in C# has different sematics than one might be used to from languages like C++. In C++ the "new" operator always allocates memory on the heap. In C# the new keyword doesn't necessarily do that. "new" is required whenever you want to call a constructor.

When you construct a value type (structs) with "new" no heap is required since value type variables always have their required size. So a local Vector3 variable, which lives on the stack, is "created" at the moment you enter the method by for$$anonymous$$g a stackframe.

When you construct a reference type with "new" it automatically reserves memory on the heap for that new instance. Reference types can't live on the stack, they are always on the managed heap. References can only reference heap memory and never stack memory.

Value types (structs as well as primitive types like int, float, ...) can also live on the heap, but you never have to create memory manually for those. The process of placing a value type object on the managed heap happens implicitly. This process is called boxing. Boxing happens whenever you "convert" / assign a value type into the general base class "object".

 int i = 5; // lives on the stack
 object o = null; // The variable "o" also lives on the stack but as a reference type the actual memory on the stack only contains the reference / address of an object on the heap
 
 o = i; // here i get boxed automatically. So memory on the heap is allocated where the value of i will be stored and o will reference that memory.
 
 i = (int)o; // This is called unboxing. Here the value which is stored in heap memory is copied back into an actual int variable which lives on the stack.

As soon as the "o" variable goes out of extent the memory that was used for the boxed variable can be collected by the GC.

Like @Naphier said you usually don't have to call the constructor of a value type manually. For valuetypes you actually can't define a custom parameter-less constructor. That is always provided by the system.

$$anonymous$$ember variables of a class are initialized automatically when the class is created. The field initializers are called before the actual constructor of the class. If no field initializer is provided than the variables get initialized with their default value which is usually "0" for numerical primitive types and "null" for all reference types.

Local variables on the other hand aren't initialized automatically and need to be initialized expliciitly. For structs it's enough to set all of the member variables of the struct to something. However structs that have private variables must be initialized with a constructor since you can't set private variables from the outside.

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

6 People are following this question.

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

Related Questions

Using arrays of vector3s to avoid new 1 Answer

Console Errors on new project 1 Answer

Components arent being found in script references 1 Answer

Problem with reading values from scripts on GameObjects in Lists 1 Answer

Having 452 errors when create a new empt project 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