show variables in Text

Hi,

I’m trying to write with code a text that would display variables in my program (for that I use a GameObject UI and then Text).

In fact I would like to display the X and Y variables (or others like iCellHor or iCellVerInPent) of the mouse in text.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class debogage : MonoBehaviour
{
    public Text text1, text2, text3, text4;
    void Update()
    {
       {   // the variables are declared in the mouseUp file

            text1.text = "cellHor " + mouseUp.iCellHor;
            text2.text = "cellVer " + mouseUp.iCellVer;
            text3.text = "cellHorInPent " + mouseUp.iCellHorInPent;
            text4.text = "cellVerInPent " + mouseUp.iCellVerInPent;

            Debug.Log(mouseUp.iCellHor);
        }
    }
}

The problem is that the variables show 0 or nothing.

A picture speaks better by itself:

I would like to retrieve and display the values of the variables in the Text component.

I thank you for your help,

A+

Are the values 0? :eyes:

1 Like

The x and y variables are the position of the mouse in the screen, so I think that when I move the cursor, the values may not be 0.
Moreover the CelHor and CellHorInPent and the other variables may be between 1 and 10.
I don’t know why the Text component, shows nothing or only 0 value.

If you can help me to show the coordinates of the mouse in the screen with 2 text UI component (one for x and other for the y)?

Thanks,

A+

Hi there,

After some efforts I managed to display the x and y variables (see code below),

but not the NumberToShow variable.

public Text text1, text2;

    void Update()
    {
          text1.text = "x =  " + Input.mousePosition.x;
          text2.text = "y =  " + Input.mousePosition.y;
     }

And this is the code I’m trying to get to work :

    public Text text1;

    void Update()
    {
        text1.text = "variable =  " +File.NumberToShow;
    }

}

And this is the code I’m trying to get to work.

The problem is that the NumberToShow variable displays nothing or a 0.

I want to display the number of a variable in another file in a UI Text component.

The file File is a script which has for variable public static int NumberToShow.

NumberToShow is the variable I want to display in the UI text component.

If you can help me,

A+

Show the code where you change the value from the default of 0.

Here are my variable :

public Text text1, text2, text3, text4;
Update () text1.text = "cellHor " + mouseUp.iCellHor;
            text2.text = "cellVer " + mouseUp.iCellVer;
            text3.text = "cellHorInPent " + mouseUp.iCellHorInPent;
            text4.text = "cellVerInPent " + mouseUp.iCellVerInPent;

mouseUp is the file that contains the variables( cellHor, cellVer,…).

Thank to you.

Just to make sure: these are public static variables right? If they are not static, you can not access a variable through its class and instead must work with an instance of that class.
It may also help if you posted all related code. Like the mouseUp script, or the one that fills its variables with content. This problem likely has nothing to do with the text part at all.

Further, regarding readability of the code, i would advice:

  • Look up C# naming conventions. People expect classes to have UpperCamelCase type names. Same with methods and properties. Variables are then written in camelCase and so on. Just to name a few. This immensely helps with identifying what you are looking at right from the get go. It also helps other programmers read your code.
  • Code in english. You already work with an API (Unity) in english, so at best you are mixing two languages. But in any case where you can not guarantee that you are the only person to ever read your code, you should code using english variable names. And that is pretty much always the case, ever. I just read your code, for example. For people not native in your language, your variables may as well be called A, B, C and so on. For more complex scripts this makes reasoning about them nearly impossible. When you post anything more complex with variables people cant reason about, they simply wont bother helping.

Hello,

My question is not too complicated I just want to display variables in an UI Text component.

Many thanks,

A+

Grozzler’s question is not too complicated either:

I suspect when you answer Grozzler’s question you will also have answered yours.

1 Like

Hi,

Here is the code :

Update ()
{
text1.text = "var" + mouseUp.newVar;}
}

My variables are stocked in a script called mouseUp()
And this script is in a file called Debogage (in the Update code)

Show the mouseUp script already, this is getting ridiculous. Are you just straight up trolling?

1 Like