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 /
avatar image
0
Question by RafiXWPT · Aug 26, 2015 at 01:41 PM · listclassinitialization

initialize (variable/List in Start() or Class?)

Hello, Yesterday I was facing really strange problem, and I want to know, why.

I have 2 scripts: ContainerSlot, & ContainerManager.

Firstly I had:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class ContainerSlot: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IPointerClickHandler {
 
     public ItemType equipmentPart;
     public Sprite backgroundImage;
 
     Item item;
     public Item Item {
         get {
             return item;
         }
         set {
             item = value;
         }
     }
 
     Image icon;
     
     void Start () {
         item = new Item();
         icon = transform.GetChild(0).GetComponent<Image>();
         icon.sprite = backgroundImage;
     }
 }

And I don't know why, when I was trying to grab Item in ContainerManager, ContainerSlot returned null all time.

When I changed script to:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class ContainerSlot: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IPointerClickHandler {
 
     public ItemType equipmentPart;
     public Sprite backgroundImage;
 
     Item item = new Item();
     public Item Item {
         get {
             return item;
         }
         set {
             item = value;
         }
     }
 
     Image icon;
     
     void Start () {
         icon = transform.GetChild(0).GetComponent<Image>();
         icon.sprite = backgroundImage;
     }
 }

Everything works fine and ContainerManager can grab Item. I checked in ScriptExecutionOrder and ContainerSlot was BEFORE ContainerManager, so there sould be no problems. I had that problems in some scripts with Lists too, when was initialized in class, everything was ok, if in Start(), null exceptions spam my console.

Now Question. Is it better to always initialize variable/List inside Class to some default value? Or its only me fighting with some unknown issue?

Comment
Add comment · Show 6
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 Scribe · Aug 26, 2015 at 02:15 PM 0
Share

new Item() overrides what is saved in it an makes a new instance which is equal to null. You have to set it to something after the new Item() call.

avatar image _Gkxd · Aug 26, 2015 at 02:56 PM 0
Share

@Scribe

I don't think this is true. I believe that using the new keyword with a class causes a memory allocation on the heap, and the returned value is the reference to the memory location on the heap. Null, on the other hand, means that the referenced memory location is 0, which is an invalid memory address and defined as a null pointer.

When you use new, the constructor of the class is called, and the value that you get is a reference to the instance of that class, initialized with the constructor.

@RafiXWPT

I personally dislike line 11 of the second piece of code. You don't know when this line gets called (though it is possible to look this up in some reference). I think it's better to put it in a constructor or the Start/Awake functions so that you explicitly know when you are initializing that field.

For why you may be getting null pointers when you put that line inside Start/Awake, you are probably trying to use that value before Start function that initializes the value is called. If you need a value to be initialized by the Start function, you can initialize it in Awake, which is called before Start. It's difficult to actually know what the problem is because there aren't any concrete examples. In the code that you gave, you aren't using item anywhere.

avatar image RafiXWPT · Aug 26, 2015 at 03:19 PM 0
Share

@_Gkxd

Yeah, for me that second solution is also bad, I don't like that. I tried with Awake() and with it everyting is okey so Ill move all inits there, but tell me, why Awake() works and Start() no, even when inside ScriptExecOrder that script should run first?

avatar image Scribe · Aug 26, 2015 at 03:22 PM 1
Share

@_Gkxd You are correct, I misread the getter setter, and thought he was simply overwriting a value that had already been assigned!

possibly as an alternative you could do:

 Item item;
 public Item Item {
     get {
         if(item != null) return item;
 
         item = new Item();
         return item;
     }
     set {
         item = value;
     }
 }
avatar image RafiXWPT · Aug 26, 2015 at 03:26 PM 0
Share

@Scribe Yes! Thats that, I never had such problem before with get/set. Can you set your comment as answer? Ill accept it cus after checking null in getter as you suggested, everything back to normal :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Scribe · Aug 26, 2015 at 03:48 PM

It appears they have increased the karma needed to 10,000 to convert things which is unfortunate, so I will just copy it across!

possibly as an alternative you could do:

 Item item = new Item();
 public Item Item {
     get {
         if(item != null) return item;
 
         item = new Item();
         return item;
     }
     set {
         item = value;
     }
 }
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 RafiXWPT · Aug 26, 2015 at 04:49 PM 1
Share

in this approach on 1st line item = new Item(); is not needed ;)

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

27 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

Related Questions

A node in a childnode? 1 Answer

Collisions and Lists 1 Answer

Integer List values don't get initialized in Constructor of Class 2 Answers

Orderrring a List Ascending or Descending C# Advanced 2 Answers

[CLOSED] When I use this code that I made, it adds to the item catalogue too? [CLOSED] 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