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 Pleroma · Aug 25, 2016 at 11:46 AM · variablesstaticinitialization

Static Class Strings not truly null?

I have a Class for my game that controls the player's hand. It has a boolean called handFull, and when it is true the button that causes the player to grab an object doesn't work. The hand is full, see?

I also have a seperate Class that handles persisting data between scene transitions. It handles player inventory and triggers for the game at large. It is called Persistence. I built it with a public static variable of itself, so that it can be accessed by other Classes in the game (If you've watched the Unity tutorial on Data Persistance, I basically have that, with more variables being remembered) . The player has one inventory slot, and it is handled by a string.

I want the handFull state to persist between scene changes. After all, if you have a Sword in your inventory and your hand is full (handFull == true), it wouldn't do if you shifted scenes, the sword remained in your inventory, but handFull (handled by a non-persistant Class) is suddenly false.

So I wrote some code. It looked like this:

 void Awake (){  
 If (Persistance.mem.Inventory == null){  
 handFull = false  
 }else{  
 handFull = true;  
 }

But every time I ran the script for testing, the game loaded, and handFull was set to true. I went to the Persistance Class, and defined the string Inventory as null, but it did not work. Eventually, I ended up creating a long list of 'Or' statements (if he's not holding a sword or a shield or a bottle or a... then the hand is empty). It works, for this game, but I'd like to have a bit more elegance in my future projects.

What the hell did I do wrong?

Comment
Add comment · Show 4
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 DiegoSLTS · Aug 25, 2016 at 12:10 PM 0
Share

Are you sure nothing is settings a non-null value to inventory? Show the inventory value in the console and you might see what's changing it

avatar image Pleroma DiegoSLTS · Aug 25, 2016 at 06:25 PM 0
Share

Well, I thought nothing was setting it to null, as I set up the variable inventory as = null; initially. But clearly I was wrong. How would I check it in the console?

avatar image Grendl · Aug 25, 2016 at 01:31 PM 0
Share

Is handFull defined as a public variable? If so, it is possible that Unity serializes the value somewhere in the editor, causing the value to be set when you start the game.

avatar image Pleroma Grendl · Aug 25, 2016 at 06:23 PM 0
Share

It is a public variable, but I am pretty sure it's the script that's flipping it to true, since if I remove the script I can set it to whatever I want and it stays unchanged.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gcoope · Aug 25, 2016 at 01:21 PM

Instead of if(Persistance.mem.Inventory == null) try using if(string.IsNullOrEmpty(Persistance.mem.Inventory)) to check the status of the string. This does as you would expect, checking that that object isn't null, as well as checking if the value of it isn't blank

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 Pleroma · Aug 25, 2016 at 06:28 PM 0
Share

Thank you! This does what I need to do. Though I am still curious why a string initialized as = null; isn't reading as = null;.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my script only running when object has been selected beforehand? 1 Answer

Setting public static variables in a static class 2 Answers

Change of static variable 1 Answer

Game Objects not using same script independently. 1 Answer

Scene stores variables after scene reload 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