Search Unity

Unity 2019.2.21 to 2021.1 error. Need help upgrading

Discussion in 'Scripting' started by RandomCharacters, Apr 30, 2021.

  1. RandomCharacters

    RandomCharacters

    Joined:
    Nov 29, 2012
    Posts:
    262
    I have an old project that partly was done from someone else. It was last done on 2019.2.21. Now when I open it with unity 2021.1 it gives some errors. If someone could help on the translation that would be cool. I f needed, I can message you with the full scripts. Unity doesn't like it when people post like that. The lines with errors say //error.


    Code (CSharp):
    1. ////
    2. /////////////////////////////////////
    3. public class Joystick : MonoBehaviour {
    4. private GUITexture gui;                // Joystick graphic (ERROR)
    5.  
    6.  
    7. ///////////////////////////////////////////
    8. void Start()
    9.     {
    10.         // Cache this component at startup instead of looking up every frame  
    11.         gui = GetComponent<GUITexture>(); //(ERROR)
    12.  
    13.         defaultRect = gui.pixelInset;    //(ERROR)
    14.  
    15.         defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5;
    16.         defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5;
    17.  
    18.         transform.position = Vector3.zero;
    19. //        transform.position.x = 0.0f;
    20.      //   transform.position.y = 0.0f;
    21.          
    22.  
    23. ////////////////////////////////////////////////////
    24.         if ( touchPad )
    25.         {
    26.             // If a texture has been assigned, then use the rect from the gui as our touchZone
    27.             if ( gui.texture )  //ERROR
    28.                 touchZone = defaultRect;
    29.         }
    30.         else
    31.  
    32. ////////////////////////////////////
    33. void setColor(float r)
    34.     {
    35.         Color c = gui.color;  //ERROR
    36.      
    37.         c.a=r;
    38.         //c.g=g;
    39.         //c.b=b;
    40.         if(gui.color!=c) //ERROR
    41.         {
    42.             gui.color=c;    //ERROR
    43.         }  
    44.     }
    45.  
    46. ///////////////////////////////////
    47. void ResetJoystick()
    48.     {
    49.         // Release the finger control and set the joystick back to the default position
    50.         gui.pixelInset = defaultRect;  //ERROR
    51.  
    52. //////////////////////////////////////////////
    53. void OnMouseOver()
    54.     {
    55.         if(m_on)
    56.         {
    57.             Color tmp = gui.color;  //ERROR
    58.             tmp.a = 0.35f;
    59.             gui.color = tmp;  //ERROR
    60.         }
    61.                      
    62.     }
    63. ///////////////////////////////////////////////////////
    64.     void OnMouseExit()
    65.     {
    66.         if(m_on)
    67.         {
    68.             Color tmp = gui.color;  //ERROR
    69.             tmp.a = 0.25f;
    70.             gui.color = tmp; //ERROR
    71.         }
    72.     }
    73. /////////////////////////////////////////////////////
    74. if ( touchPad )
    75.                 {              
    76.                     if ( touchZone.Contains( touch.position ) )
    77.                         shouldLatchFinger = true;
    78.                 }
    79.                 else if ( gui.HitTest( touch.position ) )  //ERROR
    80.                 {
    81.                     shouldLatchFinger = true;
    82.                 }
    83.  
    84. //////////////////////////////////
    85.  
    86. else
    87.                     {                  
    88.                         // Change the location of the joystick graphic to match where the touch is
    89.                         Vector2 gmin = guiBoundary.min;
    90.                         Vector2 gmax = guiBoundary.max;
    91.                         Rect tmpRect = gui.pixelInset;  //ERROR
    92.                      
    93.                         tmpRect.x = Mathf.Clamp( guiTouchPos.x, gmin.x, gmax.x );
    94.                         tmpRect.y = Mathf.Clamp( guiTouchPos.y, gmin.y, gmax.y );      
    95.                         gui.pixelInset = tmpRect;  //ERROR
    96.                     }
    97. ////////////////////////////
    98. if ( !touchPad )
    99.         {
    100.             // Get a value between -1 and 1 based on the joystick graphic location
    101.             position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;  //ERROR
    102.             position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;  //ERROR
    103.         }
    104. ///////////////////////////////////
    105.  
    106.  
    107.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    It might be that under the new Unity you need to install a package for the old GUI system (like GUITexture)... that would be one place to check first. Here is some more reading:

    https://answers.unity.com/questions/1638555/guitexture-adn-guitext-are-obsolete-standard-asset.html

    Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The important parts of an error message are:
    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855
     
  3. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Try look at where each error is. Have you noticed a pattern where each error is? Look into it with more detail
     
  4. RandomCharacters

    RandomCharacters

    Joined:
    Nov 29, 2012
    Posts:
    262
    "Try look at where each error is."
    CS0619. The issue is the GUItext, GUItexture and all of that was depreciated to the new system. I was wondering if someone had the conversion.

    "Unity you need to install a package for the old GUI system"
    Where would this be and what would it be called? I looked under the packages and do not see this.
     
  5. RandomCharacters

    RandomCharacters

    Joined:
    Nov 29, 2012
    Posts:
    262
    Here's 1 example. The gt.pixeloffset gives an error. So the conversion to just "gt" doesn't work. Text obviously is not the proper thing

    Code (CSharp):
    1.  
    2. public static void resizeGUIText(GUIText gt, Vector2 vec)
    3.     {
    4.         if(gt!=null)
    5.         {
    6.             Vector2 v = gt.pixelOffset;
    7.             v.x *= vec.x;
    8.             v.y *= vec.y;      
    9.             gt.pixelOffset = v;
    10.         }
    11.     }
    Code (CSharp):
    1.  
    2. public static void resizeGUIText(Text gt, Vector2 vec)
    3.     {
    4.         if(gt!=null)
    5.         {
    6.             Vector2 v = gt.pixelOffset;
    7.             v.x *= vec.x;
    8.             v.y *= vec.y;      
    9.             gt.pixelOffset = v;
    10.         }
    11.     }
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Just looked into this more... GUIText and GUITexture are gone now. You would need to convert to using the UnityEngine.UI namespace, which also now comes in a package.

    If you have like 5 of these things, just remake the UI. There's tons of tutorials on the "new" UI, which is actually new in 2014, so it's already 7 years old.

    If you have like 500 of these things, it might be worth trying to write some kind of automation to trans-map them into some kind of emulated GUI layer, but it would be some work.
     
  7. RandomCharacters

    RandomCharacters

    Joined:
    Nov 29, 2012
    Posts:
    262
    thanks for the info. that all seems logical. So how does one actually automate this with some kind of GUI layer? and where are these tutorials? I have looked and found the guitext conversion but not much with guitexture.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Google UnityEditor tutorials.

    I imagine you'd make this into a UI.Image object.

    The GUIText would probably become a UI.Text or a TextMeshPRO thing