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 KacperGorgon · Feb 22, 2021 at 10:07 PM · scripting beginnerstringnull

Null string from another script

I'm trying to get the string from the DateTest.cs to DayCheck.cs script, but while doing that the string become null. Is there is any fix?

DateTest.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DateTest : MonoBehaviour
 {
     public Text dataText;
 
     public int iDays = System.DateTime.UtcNow.ToLocalTime().Day;
     public int iMonths = System.DateTime.UtcNow.ToLocalTime().Month;
     public int iYears = System.DateTime.UtcNow.ToLocalTime().Year;
     public int iHour = System.DateTime.UtcNow.ToLocalTime().Hour;
     public int iMinute = System.DateTime.UtcNow.ToLocalTime().Minute;
     // "z" is for the test
     public string days = "z", months, years, hours, minutes, seconds;
 
     void Update()
     {
         string days = System.DateTime.UtcNow.ToLocalTime().ToString("dd");
         string months = System.DateTime.UtcNow.ToLocalTime().ToString("MM");
         string years = System.DateTime.UtcNow.ToLocalTime().ToString("yyyy");
 
         string hours = System.DateTime.UtcNow.ToLocalTime().ToString("HH");
         string minutes = System.DateTime.UtcNow.ToLocalTime().ToString("mm");
         string seconds = System.DateTime.UtcNow.ToLocalTime().ToString("ss");
 
         Debug.Log("days from DT: " + days);
 
         dataText.text = "Today is " + days + "-" + months + "-" + years + " | " + hours + ":" + minutes + ":" + seconds;
     }
 }

DayCheck.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DateTest01 : MonoBehaviour
 {
     public GameObject DateTestScript;
     DateTest string1;
 
 public void OnButtonPressed()
     {
         string1 = DateTestScript.GetComponent<DateTest>();
 
         string cosar = string1.days;
         // Console shows: string1:  (probably null)
         Debug.Log("string1: " + cosar);
     }
 }
 
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
1
Best Answer

Answer by Bunny83 · Feb 23, 2021 at 03:55 AM

You redeclared all your variables as local variables inside your Update method. So you never actually assigned any values to your class variables. You should change your Update method to:

 void Update()
 {
     var time = System.DateTime.UtcNow.ToLocalTime();
     days = time.ToString("dd");
     months = time.ToString("MM");
     years = time.ToString("yyyy");

     hours = time.ToString("HH");
     minutes = time.ToString("mm");
     seconds = time.ToString("ss");
     dataText.text = "Today is " + days + "-" + months + "-" + years + " | " + hours + ":" + minutes + ":" + seconds;
  }

Note that the important thing is that I removed all the string types in front of each line. Now we assign the actual class variables instead of declaring new local variables. I also read the current time once. Reading UtcNow 6 times in a row can result in out of sync reading. So for example around 8:59 you may read an hour value of 8 but a minute value of "00" because at the time the minute reading is happening the time may have advanced already to 9:00.

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 KacperGorgon · Feb 23, 2021 at 12:32 PM 0
Share

Thank you! It worked!

avatar image
1

Answer by CodesCove · Feb 23, 2021 at 03:12 AM

If DateTest component is already attached to the gameobject at the start or it's stored in a prefab and instantiated (actually more like cloned) later, then the "days" value will be what is serialized in the Inspector. When setting the value in the class definition (to "z" in this case) will not have any affect.

So it's not null (you would get null exception otherwise) but just empty (like the probably in the inspector).

So you can set the initial value in the inspector or inside Start or Awake method that are always called automatically when the game starts.

However if you want the "days" variable not be serialized and still accessible from other classes you need to add [System.NonSerialized] attribute before the variable. That way the Inspector will not show the variable and will not set any value to it (meaning you can set it as you like in the class definition).

So for example one working way would to add this to DateTest.cs

 void Start() 
 {
       days = "z";
 }

Other way:

 [System.NonSerialized]
 public string days = "z";
Comment
Add comment · Show 2 · 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 CodesCove · Feb 23, 2021 at 12:26 PM 0
Share

My answer explains this specific case why you are getting empty string. Bunny83 has the correct and generic root cause answer.. as always :)

avatar image KacperGorgon CodesCove · Feb 23, 2021 at 12:32 PM 0
Share

Thank you anyway!

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

125 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 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

PlayFab CloudScript NullReferenceException 0 Answers

Default string value... empty? (C#) 2 Answers

String gives me Null,String gives me Null 1 Answer

TextMeshPro " != " not working 1 Answer

How to get multiple string values from override ToString()? 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