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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

parse int to stylelength

Discussion in 'Scripting' started by divvelaRavi, May 6, 2022.

  1. divvelaRavi

    divvelaRavi

    Joined:
    Jul 8, 2020
    Posts:
    9
    Example of my code: visualElement.style.top = int * buttonVisualElement.style.height;

    The error I'm receiving is: Operator '*' cannot be applied to operands of type 'int' and 'StyleLength'

    I'm trying to move a visual element by changing its top value.
    The value should be the product of button index position * button height.
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Perhaps int isn't the name of a variable?
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    Because the height property of a style is of type StyleLength. This is a struct which has a value field which is of type Length. Length is also a struct which again has a value field which actually is a float value. So I guess you want to set this float value. Since top is also a property of type StyleLength and that's a struct, you probably want to use the constructor of StyleLength which has an overload that just takes a float. So try something like

    Code (CSharp):
    1. visualElement.style.top = new StyleLength(int * buttonVisualElement.style.height.value.value)
    Keep in mind that those structs are there for a reason. The keyword may constrol how or if the value is used at al. Likewise the Length struct has a unit setting to indicate of the actual float value is a pixel value or a percentage value.

    It's difficult to tell what's your exact usecase. However you may just read a bit more in the documentation :)
     
  4. divvelaRavi

    divvelaRavi

    Joined:
    Jul 8, 2020
    Posts:
    9
    This is my actual code "m_ActiveTabIndicator.style.top = bindex * clicker.style.height;" for better understanding I wrote it like that.
     
  5. divvelaRavi

    divvelaRavi

    Joined:
    Jul 8, 2020
    Posts:
    9
    CSharp]visualElement.style.top = new StyleLength(int * buttonVisualElement.style.height.value.value)

    This had No error but the desired result is not there. Nothing was happening.

    I tried this "m_ActiveTabIndicator.style.left = bindex * 100;" & it is working but I don't want to hardcode the value.

    bindex is still an int, I tried a value of 100 in the place of Quering height. it doesn't make sense to me how it is working. Is there any other workaround?
     
    Last edited: May 6, 2022
  6. divvelaRavi

    divvelaRavi

    Joined:
    Jul 8, 2020
    Posts:
    9
    So, the answer is
    Code (CSharp):
    1. m_ActiveTabIndicator.style.left = bindex * int.Parse(clicker.resolvedStyle.width.ToString());
     
  7. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Last edited: May 9, 2022
  8. NeverSnows

    NeverSnows

    Joined:
    Jul 16, 2022
    Posts:
    1
    So i'm having the same issue. I'm trying to make a tab adjust its height depending on the amount of buttons it has.
    I've tried that already:
    Code (CSharp):
    1. btnPannel.style.height = uiButtons.Count * int.Parse(btnPannel.resolvedStyle.width.ToString());
    But it didn't work, so i've tried to print btnPannel.resolvedStyle.width.ToString(), but it says "NaN" (Not a Number).
    Any clue as to why?
     
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    don't you find this at least a little bit silly? and ugly? and useless?
    I know this wasn't your idea, I can see the original post, but imagine if I was to convert this message of mine into Alien NeoJapanese, then translate it back. you might still understand it, but at what cost? in other words don't unnecessarily convert values to strings for no apparent reason.

    now, why don't you try
    Debug.Log(btnPannel.resolvedStyle.width)
    and see what you'll get?
    then try
    Debug.Log(btnPannel.resolvedStyle.width.value)
    and then
    Debug.Log(btnPannel.resolvedStyle.width.value.value)


    we already know the structure of these elements, and we know that they hold a float (number) down the line, so why don't you test these assumptions and see what's going on? once you've done this, come back with the results and we can troubleshoot this together.

    edit:
    btw if a system tells you something's a NaN (not a number), it really means it, and it has to be true, at least according to some specification on what is classified as number and what is not.