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
0
Question by kevinrocks_786 · Dec 12, 2015 at 10:25 PM · unity 5objectclassclassesunityobject

Custom type Objects without hte " = new "

Hello guys, I was wondering why is it that when I create a custom class, I have to declare an object like this:

 Person Bob = new Person;

But with Unity's custom classes I don't; Example:

 puiblic RaycastHit hit;
 public Color myColor;
 public RigidBody rB;
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Dec 13, 2015 at 01:39 AM

First of all RaycastHit and Color aren't classes. They are structs and therefore valuetypes. Like LazyElephant already mentioned your own classes are not really different from built in classes.

Any kind of variable need to be initialized in some way before you can use it's content. Class-variables or more general reference type variables just hold a reference to an instance of it's type. So in your case the variable Bob is of type "Person". If Person is a class then the variable Bob can hold a reference to an instance of your class Person. The default value for reference types is "null", so it doesn't reference any object. In your example you create a new instance of your Person class with "new" and store the reference to that instance in your variable Bob. So when using the variable Bob you can access that instance. That's how classes work in general.

However the Unity editor has a feature for some supported classes, in general classes which can be serialized by Unity, where you don't have to create a class manually but the editor might either create an instance for you or allows you to drag and drop an existing instance onto the variable in the inspector. In your example the Rigidbody type is a built-in component class. The variable, if nothing is assigned will also have the default value of "null" and using it will cause an exception since it doesn't reference any object which could be accessed. Such a variable can be assigned in the inspector by dragging an existing component onto the variable in the inspector. You effectively store the reference to that component instance in the variable.

The case where the editor creates an instance for you only applies to custom classes which have the System.Serializable attribute. For example if you declare a type like this:

 [System.Serializable]
 public class Person
 {
     public string name;
     public float age;
 }

Whenever you have a variable of type Person in a MonoBehaviour, the Unity editor will automatically create an instance for you. So here you don't need to create an instance manually with "new". In general the Inspector can only view / edit things which are serializable by Unity. So the Person instance is actually serialized within the MonoBehaviour instance.

Finally value types like Vector3, RaycastHit, Color, Rect, ... can't have "no value" like reference types since the variable directly hold the data. If you declare a Vector3 variable in a class, its members are automatically initialized with the default value. A Vector3 is just a struct with 3 float variables. The default value for a float is 0f. The Color struct is actually almost the same as a Vector4 but has different names for its variables (r, g, b, a instead of x, y, z, w). So the default value is (0f, 0f, 0f, 0f).

Since all those built-in Unity types are serializable, the inspector will show a color field and allows you to change the color in the inspector.

Note: This all only applies to member variables of a class. If you have local variable inside a method you actually have to assign a value before you can use the variable. Local variables aren't initialized with the default value automatically.

 void SomeMethod()
 {
     Color col;
     Debug.Log(col); // error since "col" hasn't been initialized
 }

Finally a short hint about MonoBehaviour derived classes. First of all a class is always a class and it always works the same way. The MonoBehavíour base class however has a special meaning to Unity. A MonoBehaviour is a component. Components are special classes which need to be attached to a GameObject instance. They can't "live" on their own. That's why you have to use the AddComponent method of the GameObject class to manually create an instance of a MonoBehaviour / Component class. You still can create an instance of a MonoBehavíour class with "new", but you can't do anything useful with the instance. Unity will also throw some warnings at you that you shouldn't create Components with "new".

When you drag a MonoBehaviour script file onto a gameobject in the editor, the editor actually calls AddComponent with the class that is defined in that file. The AddComponent method will create an instance of your class internally and attach it to the GameObject.

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 LazyElephant · Dec 13, 2015 at 03:23 AM 0
Share

+1, that's a thorough answer

avatar image
0

Answer by LazyElephant · Dec 12, 2015 at 11:02 PM

Unity classes behave in the same way as your custom class. In your example, you're both declaring your person variable and assigning a reference to a new person on the same line. With the Unity classes in your example, you're only creating the variables without assigning them to anything.

Before you could use the rigidbody, for example, you would have to assign a rigidbody object to it like rb = new Rigidbody (); Or by assigning an already existing object.

Edit: I was unaware that Color is actually a struct, so I changed the example to use the rigidbody.

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 Owen-Reynolds · Dec 13, 2015 at 01:31 AM 1
Share

Well, Color is a struct, so you don't need to use new. A new for structs just calls the constructor, and doesn't actually allocate space like a real new (you can look it up. It's just the way they make you call the constructor. The struct/class rules for C# are needlessly silly.)

Raycasthit is also a struct, I think.

But for for most things, which are classes, you do need the new. The trick is probably to just read about C# struct vs. class rules.

avatar image LazyElephant Owen-Reynolds · Dec 13, 2015 at 03:14 AM 0
Share

That's good to know. I've seen examples where colors were created with new, so I assumed it was a class. Thanks for the correction.

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

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

Related Questions

How to make objects without the new keyword 0 Answers

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

Create group people, Select one or more at random. 1 Answer

Trouble with creating objects 1 Answer

NullReferenceException: Object reference not set to an instance of an object 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