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

Using a variable from Start for Update

Discussion in 'Getting Started' started by Oreo-Thelewdcookie, Sep 29, 2022.

  1. Oreo-Thelewdcookie

    Oreo-Thelewdcookie

    Joined:
    Sep 30, 2018
    Posts:
    5
    Henlo, i'm fairly new to C# and Unity and was trying to scale my background along with the Canvas without using anchor point and breaking the pixel (stretching, etc)

    I figured out a basic formula that will help me solve the task by scaling the renderTexture .
    By dividing the "new" RectTransform in Update , with "old" RectTransform in Start , in my case it was 3840 / 1920 = 2 (3840 is default resolution *Start* and 1920 after i changed the resolution *UPDATE*)
    I then will use the "result" for the scaling, thus matching the canvas size .

    It works in theory and manual adjustment, but i can't get the code to work for automation .
    the code did compile but Unity editor gave out an error "
    NullReferenceException: Object reference not set to an instance of an object
    MenuBackgroundScaler.Update () (at Assets/Scripts/MainMenu/MenuBackgroundScaler.cs:45)
    ".
    I understand that this error is referencing to a null/empty variable .
    "oldrt" to be exact .

    I think the problem comes down to me not knowing how to use a result from Start and bring it to Update


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class MenuBackgroundScaler : MonoBehaviour
    7. {
    8.  
    9.     public Canvas canvas;
    10.     public GameObject menubackground;
    11.  
    12.     private RectTransform oldrt;
    13.    
    14.     void Start()
    15.     {
    16.         //Fetch the RectTransform from the GameObject
    17.      
    18.         canvas = gameObject.GetComponent<Canvas>();
    19.         Debug.Log (canvas.enabled);
    20.  
    21.         //old canvas size
    22.         RectTransform oldrt = canvas.GetComponent<RectTransform>();
    23.      
    24.         oldrt.sizeDelta = new Vector2(oldrt.sizeDelta.x, oldrt.sizeDelta.y);
    25.      
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         //getting the rect values
    31.  
    32.         RectTransform rt = canvas.GetComponent<RectTransform>();
    33.  
    34.         //formula
    35.  
    36.  
    37.         if (rt.sizeDelta == new Vector2(3840f, 2160f) || rt.sizeDelta == new Vector2(5120f, 2160f))
    38.  
    39.         {
    40.             menubackground.transform.localScale = new Vector3(oldrt.sizeDelta.x/rt.sizeDelta.x, oldrt.sizeDelta.x / rt.sizeDelta.x, oldrt.sizeDelta.x / rt.sizeDelta.x);
    41.         }
    42.         else
    43.         {
    44.             menubackground.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    45.         }
    46.     }
    47. }
    48.  
    howto.jpg

    Note: i could just put in the actual result, but doing so mean i will have to manually calculate for all the different resolution, plus i'm still in the process of learning, so knowing how to get this to work would be nice, please help
     
  2. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    The end result you've already got's quite good actually, I don't think you need to worry too much :)
     
  3. Oreo-Thelewdcookie

    Oreo-Thelewdcookie

    Joined:
    Sep 30, 2018
    Posts:
    5
    but how can i bring the oldrt variable from Start to Update :( , the code did compile but Unity gave out a null error, meaning the result of oldrt variable didn't transfer from Start to Update at all
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    In Start, change
    RectTransform oldrt =
    to
    oldrt =
    . You're redeclaring it otherwise.. so the value is assigned to the new one in the function, instead of the one you've declaring outside of the function (which Update will be using).
     
  5. Oreo-Thelewdcookie

    Oreo-Thelewdcookie

    Joined:
    Sep 30, 2018
    Posts:
    5
    Thanks you very much !!! it works like charm , now my background is scaling with every different resolution setup, no need to manually calculate it anymore !
     
    Last edited: Sep 29, 2022
    adamgolden likes this.
  6. Oreo-Thelewdcookie

    Oreo-Thelewdcookie

    Joined:
    Sep 30, 2018
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. public class MenuBackgroundScaler : MonoBehaviour
    6. {
    7.     public Canvas canvas;
    8.  
    9.     public GameObject menubackground;
    10.     private RectTransform oldrt;
    11.     private RectTransform newrt;
    12.     private float oldx;
    13.  
    14.     private void Awake()
    15.     {
    16.  
    17.     }
    18.     void Start()
    19.     {
    20.         //Fetch the RectTransform from the GameObject
    21.         canvas = gameObject.GetComponent<Canvas>();
    22.         Debug.Log(canvas.enabled);
    23.         //old canvas size
    24.         oldrt = canvas.GetComponent<RectTransform>();
    25.         oldrt.sizeDelta = new Vector2(oldrt.sizeDelta.x, oldrt.sizeDelta.y);
    26.         Debug.Log("oldrt value" + oldrt.sizeDelta.x);
    27.         //transfer result of oldrt to oldx
    28.         oldx = oldrt.sizeDelta.x;
    29.         Debug.Log("oldx" + oldx);
    30.     }
    31.     void Update()
    32.     {
    33.         //getting the rect values and scaling
    34.        AutomaticSetScale();
    35.     }
    36.     private void AutomaticSetScale()
    37.     {
    38.        float newvalue;
    39.    
    40.         newrt = canvas.GetComponent<RectTransform>();
    41.         newrt.sizeDelta = new Vector2(newrt.sizeDelta.x, newrt.sizeDelta.y);
    42.         newvalue = newrt.sizeDelta.x / oldx;
    43.  
    44.         Debug.Log("rt value" + newrt.sizeDelta.x);
    45.         if (newrt.sizeDelta == new Vector2(1920f, 1080f) || newrt.sizeDelta == new Vector2(2560f, 1080f))
    46.         {
    47.             //background native is 4K ( 5120 x 2160) scale it to 0.5f for 1080p
    48.             menubackground.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    49.         }
    50.         else
    51.         {
    52.             //scaling value
    53.             //formula
    54.             /*By dividing the "new" RectTransform in Update , with "old" RectTransform in Start , example 3840 / 1920 = 2 (3840 is default resolution *Start* and 1920 after changed the resolution *UPDATE*)
    55.             .*/
    56.        
    57.             menubackground.transform.localScale = new Vector3(0.5f * newvalue, 0.5f * newvalue, 0.5f * newvalue);
    58.             Debug.Log("newvalue =" + newvalue);
    59.         }
    60.     }
    61. }
     
    Last edited: Sep 29, 2022