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 Pec3t · Dec 22, 2014 at 08:03 PM · classnewserializable

Using reference without object (no new)?

I have question for code from this tutorial: http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player There is class definition for Boundary. There is reference created (called boundary). But there is no "boundary= new Boundary()" line.

Looks like I miss some very important (but basic) concept.

On the other hand, in another tutorial: http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes 'new' is called for class Stuff.

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

Answer by RudyTheDev · Dec 22, 2014 at 08:11 PM

You are right, and you would almost always create new classes with new.

But here it works, because Boundary is marked as Serializable and so Unity would create a new instance whenever it (de)serializes it (to be precise, whenever it (de)serializes PlayerController.boundary). So, when you look into the inspector of PlayerController, it will just "magically" be there, because opening inspector performs serialization and creates it.

It is easily possible to break this:

 public class BreakPlayerControllerBoundary : MonoBehaviour
 {
     void Start()
     {
         PlayerController playerController = gameObject.AddComponent<PlayerController>();
         Debug.Log(playerController.boundary.xMax); // NULL REF EXC
     }
 }

Here you would get a NullReferenceException on playerController.boundary, because it was never (de)serialized by Unity and so is still null.

More in-depth info: Serialization in Unity.

Comment
Add comment · Show 3 · 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 Pec3t · Dec 23, 2014 at 12:17 PM 1
Share

Thank You for your explanation. I have 2 more questions:

  • I mark class Boundary Serializable and then I create an object with new. Does it make any sense? Will Unity still create a new instance of the class?

  • Why Unity will never (de)serialize playerController.boundary in second example? The class is marked as Serializable.

avatar image RudyTheDev · Dec 23, 2014 at 01:42 PM 0
Share

[Serializable] does not mean that it will serialize right away (or ever). It implies that when Unity (de)serializes, one of things is that it will create any custom serializable classes ins$$anonymous$$d of keeping them null. In many cases, such as AddComponent<>, Unity does not perform serialization, so the custom class isn't created.

Unity won't overwrite your class if you create it yourself with new. In that example, you can do this:

 PlayerController playerController = gameObject.AddComponent<PlayerController>();
 playerController.boundary = new Boundary();
 Debug.Log(playerController.boundary.x$$anonymous$$ax); // Okay

You could also define it as:

 public Boundary boundary = new Boundary();

Or even better you could add it to Awake() (which happens right after AddComponent):

 void Awake()
 {
     boundary = new Boundary();
 }
avatar image Pec3t · Dec 23, 2014 at 07:46 PM 0
Share

Ok, everything's clear. Thanks again.

avatar image
0

Answer by Owen-Reynolds · Dec 24, 2014 at 05:29 PM

Unity sets up (new's, initializes) all global public vars for you. boundary is a public variable in that script: public Boundary boundary;.

What happens is, when you first add public Boundary boundary;, Unity displays it in the Inspector. That's a nice feature, so even a non-programmer can easily give it some starting values. It starts with all 0's, because it has to start somewhere.

It assumes you're going to type the correct xMin, xMax ... into the Inspector. Then, when the game runs, before the script starts, Unity new's boundary and copies in your numbers. It has to, since how else would the numbers magically get there?

Even if you never change the numbers, since it's public and you can see those 0's in the Inspector, the deal is that you get it set up with those 0's.

The exact way Unity stores the Inspector data is by serializing. If you make a public var in a class that can't be Serialized (saved,) Unity can't save the values, so there's no point showing it in the Inspector.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity - UI Dropdown uses constructor for DropDownItem 0 Answers

Serializable class variables loose Gameobject reference in inspector 0 Answers

Declare new class[].vector3[] length? 2 Answers

JS to C# class conversion issue. 1 Answer

Serialized class, Instances and access 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