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
1
Question by headkit · Apr 28, 2010 at 03:30 PM · null

setting variables to NULL

hi folks!

how do you set a variable to NULL? i just want to do some kind of reset to it.

thanx!

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

Answer by Eric5h5 · Apr 28, 2010 at 03:32 PM

myVariable = null;
Comment
Add comment · Show 4 · 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 johan-skold · Apr 28, 2010 at 03:39 PM 0
Share

To clarify: This is assu$$anonymous$$g myVariable is not a value type (struct), in which case it isn't nullable (unless you define it as such, but that's just a pain to work with and introduces boxing). Vector3 is an example, but for that you could do something like myVariable = Vector3.zero;

avatar image headkit · Apr 29, 2010 at 10:09 AM 0
Share

man, that was easy - thanx! (i only tried Null and NULL - but hey, its good to find that question and its answer here...)

avatar image liudgervr · Nov 25, 2021 at 03:45 PM 0
Share

Is there a fast way to set a big list of variables to null? There is no option to iterate to all the variables?

avatar image sacredgeometry liudgervr · Nov 27, 2021 at 12:21 AM 0
Share

What do you mean set all the variables to null? Do you mean instantiate a new object or do you mean set it to null in the collection?

Either way:

 public class Person
 {
     public string Name { get; set; } = "Some Default Name";
 
     public int Age { get; set; } = 999;
 
     public override string ToString()
     {
         return $"{Name} is {Age}";
     }
 }
 
 var people = new List
 {
     new Person 
     {
         Name = "Jacob",
         Age = 36
     },
     new Person 
     {
         Name = "Frank",
         Age = 12
     },
     new Person 
     {
         Name = "Alice",
         Age = 24
     }
 };
 
 // Do you mean this?
 for (var i = 0; i < people.Count; i++)
 {
     people[i] = default;
 }

 //  (Outputs: ,,) i.e null x3
 Console.WriteLine(String.Join(',', people));
 
 // Or this (Outputs: Some Default Name is 999,Some Default Name is 999,Some Default Name is 999)
 for (var i = 0; i < people.Count; i++)
 {
     people[i] = new Person();
 }
 
 Console.WriteLine(String.Join(',', people));

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

1 Person is following this question.

avatar image

Related Questions

Animation Error "Null reference exception"? 2 Answers

the prefab you want to instantiate is null 1 Answer

Why is this null? Finding a script on an object 3 Answers

if ( Transform == null) ?? Empty Transform reference 1 Answer

Accessing enum from editor script returns null 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