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 legoblaster1234 · Feb 01, 2018 at 10:03 AM · listclassstaticlists

Can Static classes have static lists?

So I'm making a debug script so I can have it output into my VR headset and see the log without taking the headset off. This is the script that is managing the log (it is not attached to any gameobject and is just in a folder):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public static class NDebug {
 
     public static List<string> log;
     
     public static void Log(string content) {
         log.Add(content);
     }
     public static List<string> GetLog() {
         return log;
     }
     
 }

But when a script tries to call Log it gives an error:

 NullReferenceException: Object reference not set to an instance of an object
 NDebug.Log (System.String content) (at Assets/NDebug/NDebug.cs:10)
 SeatScript.OnEnable () (at Assets/Scripts/SeatScript.cs:21)

and so this is my question: can static non-instanced lists exist? Is there a way for me to do what I'm trying to do without adding anything to my scene?

Thanks in advance.

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

Answer by Commoble · Feb 01, 2018 at 05:52 PM

public static List<string> log; doesn't create a list, it only creates a field to keep a list in. You need to initialize a list and assign it to that field before you can add things to it, e.g.

public static List<string> log = new List<string>();

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 legoblaster1234 · Feb 02, 2018 at 03:41 AM 0
Share

Ah, thank you. I'm so used to being able to do things like Vector3 v; to create an empty vector3 and such that I just assumed it would do the same with the list. Thanks for your help.

avatar image Commoble legoblaster1234 · Feb 02, 2018 at 05:52 AM 1
Share

The very very short version of why this works this way is that every field and variable you declare initializes to some default value; but structs are value types (like primitive ints and floats) and they generally initialize with a useable default value (ints and floats default to 0). Classes are reference types, and variables of reference types default to null. (Technically they default to zero as well; they hold the value of a memory address where the instance of your class lives, and memory address zero is the Null Reference).

avatar image ShamansDock · May 18, 2020 at 11:31 AM 0
Share

Please note that even if you are changing separate parts of the list in Update() , it can't be changed at the same time. I used a for loop to overcome this problem, but basically anything that can make a delay can be used for this.

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

76 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 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 do I modify variables from a different class that belongs to the same C# file? 1 Answer

Problem with Lists and Remove 0 Answers

Inventory slots number are wrong 0 Answers

Sorting a List {string, int} 0 Answers

Custom class list seen as "none" in inspector. 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