Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Converting from ui.text to string and back

Discussion in 'Scripting' started by DaveK032, Jun 20, 2016.

  1. DaveK032

    DaveK032

    Joined:
    Jun 20, 2016
    Posts:
    2
    I'm trying to:
    • Get the contents of a text element, and convert it to a string
    • Convert the string to an integer
    • Increase the value
    • Then convert it back to a string and then write back to the text element

    So far, I've got this
    Code (CSharp):
    1.     private int WorkerIncrease = 10;
    2.     private int WorkerDecrease = 10;
    3.     public Text ResourceLevel;
    4.     public string ResourceLevelText;
    5.     public int ResourceLevelValue;
    6.     private int CurrentValueInt;
    7.     private int NewValue;
    8.  
    9.         public void Increase()
    10.     {
    11.         ResourceLevel = GetComponent<Text>();
    12.         CurrentValueInt = int.Parse(ResourceLevel);
    13.         NewValue = CurrentValueInt + WorkerIncrease;
    14.         ResourceLevel.text = NewValue.ToString();
    15.     }
    But it's still returning an "Error CS1503 Argument 1: cannot convert from 'UnityEngine.UI.Text' to 'string'"
    Any help?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2.     {
    3.         ResourceLevel = GetComponent<Text>();
    4.         int.TryParse(ResourceLevel.text,out CurrentValueInt);
    5.         NewValue = CurrentValueInt + WorkerIncrease;
    6.         ResourceLevel.text = NewValue.ToString();
    7.     }
    8.  
     
  3. DaveK032

    DaveK032

    Joined:
    Jun 20, 2016
    Posts:
    2
    Doesn't look like that worked, kept on returning:
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756)UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621)UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144)ResourceIncreaseDecrease.AltIncrease () (at Assets/Scripts/ResourceIncreaseDecrease.cs:18)NullReferenceException: Object reference not set to an instance of an object
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Hmm, the errors you posted don't seem to be related, but try this:

    Code (csharp):
    1.     {
    2.         ResourceLevel = GetComponent<Text>();
    3.         if(ResourceLevel == null) return;
    4.         int.TryParse(ResourceLevel.text,out CurrentValueInt);
    5.         NewValue = CurrentValueInt + WorkerIncrease;
    6.         ResourceLevel.text = NewValue.ToString();
    7.     }
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    The problem with your first code is you were doing
    CurrentValueInt = int.Parse(ResourceLevel);

    ResourceLevel is a Text, you are trying to convert a text box basically into an int. The correct format is

    CurrentValueInt = int.Parse(ResourceLevel.text);

    That was the reason for that error.

    Your second error is giving you a null error, which may mean you don't have a text component on the gameobject, so getComponent doesn't work as there is nothing to get. I would say if you go back to the original code and make the fix I posted, then in the inspector double check that you dragged/dropped a Text component into the ResourceLevel variable and this should work fine for you.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    really should be using TryParse as bigmisterb suggests... it's a fair bit "safer" as it handles a bunch of exception cases for you

    https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
     
    Suddoha likes this.
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    No reason to tryParse unless you expect values that will not parse into whatever you want. Since he'll be controlling the values, parse is just fine. If the user gets to enter values into an input box, for example, TryParse would certainly be better.

    Either method will work though and really doesn't address the OPs issue, just more of a preference for his given situation.