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
2
Question by JanZagar · Oct 26, 2015 at 07:31 PM · noobnewbienew

When do i use new ?

I'm new to programing and i wanna know when to write new.

For example:

x = y; or x = new y;

Can someone explain what new means please ?

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
3
Best Answer

Answer by Socapex · Oct 26, 2015 at 08:51 PM

Ok let me simplify this as much as possible. First of all, I highly recommend you watch and follow the Unity beginner programmer videos. They are definitely going to help you, it is also a very clean, consistent and FREE way to learn programming :)

new is used for creating objects. Your example doesn't really make sense because y is a variable, not a type. Also you didn't provide us with what type you are using.

So, if you can do x = y;, you definitely cannot do x = new y; (also note that should by x = new y();).

In C#, you use new when creating objects. It is a garbage collected language, which means you do not have to release memory yourself. Issues of stack vs. heap are a more advanced concept that I would not worry too much for now, until you get a good grasp on the language. The notation comes from C++, but is used to make it clear what you want to do. Class1(); could be a function or a method call, new Class1(); makes it clear you are initializing an object.

Simply put: Use new to create objects. MyObject potato = new MyObject(); If you want to assign an object, like in your example, then you do not use new as the object is already created. You are simply copying the object and it's values potato = tomato;. Note that the reality is a little more complex, but in practice this will work as you expect.

Once you understand more concepts of programming, scope, initializing your objects etc. Then I would google various topics related to: C# references, C# new, C# structs, C# classes, C# stack heap, C# initialization. These should all be related to what you are asking, but are a little bit too advanced for now I believe.

Some doc: https://msdn.microsoft.com/en-us/library/fa0ab757.aspx http://stackoverflow.com/a/6972155 http://stackoverflow.com/a/6531692 http://stackoverflow.com/questions/433591/why-do-c-sharp-and-java-bother-with-the-new-operator

Great article on references and stack/heap. I would read the first part at least: http://www.albahari.com/valuevsreftypes.aspx

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 JanZagar · Oct 26, 2015 at 08:55 PM 0
Share

Thank you for your time :)

avatar image Socapex JanZagar · Oct 26, 2015 at 09:01 PM 1
Share

No problem :) The "new" and similar concepts are hard to understand at first. Take it one step at a time ;)

Also, remember that C# isn't only used for Unity. That means you have a plethora of great information and learning resources available on the $$anonymous$$icrosoft website, stack overflow, various forums, blogs etc. One thing to keep in $$anonymous$$d though, when doing your research, Unity uses C# 2.0, so some fancy things don't always work.

Best of luck in your program$$anonymous$$g learning! :D

avatar image JanZagar Socapex · Oct 26, 2015 at 09:08 PM 0
Share

Thx, oh and by do way i knew just a few things from C++, that's why i decided to go for C# and not for JavaScript because its more similar.

avatar image JanZagar · Oct 26, 2015 at 09:01 PM 0
Share

This is where i was co$$anonymous$$g from i didn't understand why myOtherClass = new AnotherClass(); So if i understand right, this means that myOtherClass is just a copy of a public class AnotherClass ?

private AnotherClass myOtherClass;

 void Start ()
 {
     alpha = 29;
     
     myOtherClass = new AnotherClass();
     myOtherClass.Fruit$$anonymous$$achine(alpha, myOtherClass.apples);
 }
avatar image Socapex JanZagar · Oct 26, 2015 at 09:25 PM 0
Share

I'm not sure I understand your question 100% correctly, but myOtherClass becomes an initialized object. It is a "thing", that thing is what you put inside AnotherClass.

It is a "copy" in a way, by that I mean it contains everything you put when you declared that class. In other words, if AnotherClass contains a few ints, a few strings and some floats, then myOtherClass contains those.

Just to make it clear, it is important you understand that declaring a class (`public class Potato{...}`) doesn't do anything in and of itself. You need to initialize objects of that class somewhere. In Unity this is less clear, because when you inherit $$anonymous$$onoBehavior and attach your script to a prefab (inside the editor), than it will create an object for you.

To sum it up, in your example, myOtherClass is declared as going to be an object of type AnotherClass. Then when you do the new, it is initialized and "becomes" that actual object, which lives in your computer memory somewhere. You can now do stuff with it :)

Hope this is clear?

[edit] @JanZagar, I agree that was a good decision :) C# is also useful because you can easily use for any type of other projects/applications. That means what you learn with Unity can be applied for other things. Also, learning C# will help you if you ever want to learn Java, and even go deeper in C++ and (omg) C ;)

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

33 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JavaScript taken from the web isn't working. Help 0 Answers

Steam VR one object interaction using both controllers 0 Answers

Terrain editor broken? 0 Answers

Make the player go from a hook to another hook 0 Answers

Really basic question... 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