Using a variable from Start for Update

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class MenuBackgroundScaler : MonoBehaviour
{

    public Canvas canvas;
    public GameObject menubackground;

    private RectTransform oldrt;
   
    void Start()
    {
        //Fetch the RectTransform from the GameObject
     
        canvas = gameObject.GetComponent<Canvas>();
        Debug.Log (canvas.enabled);

        //old canvas size
        RectTransform oldrt = canvas.GetComponent<RectTransform>();
     
        oldrt.sizeDelta = new Vector2(oldrt.sizeDelta.x, oldrt.sizeDelta.y);
     
    }

    void Update()
    {
        //getting the rect values

        RectTransform rt = canvas.GetComponent<RectTransform>();

        //formula


        if (rt.sizeDelta == new Vector2(3840f, 2160f) || rt.sizeDelta == new Vector2(5120f, 2160f))

        {
            menubackground.transform.localScale = new Vector3(oldrt.sizeDelta.x/rt.sizeDelta.x, oldrt.sizeDelta.x / rt.sizeDelta.x, oldrt.sizeDelta.x / rt.sizeDelta.x);
        }
        else
        {
            menubackground.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        }
    }
}

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

The end result you’ve already got’s quite good actually, I don’t think you need to worry too much :slight_smile:

but how can i bring the oldrt variable from Start to Update :frowning: , 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

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).

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 !

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class MenuBackgroundScaler : MonoBehaviour
{
    public Canvas canvas;
 
    public GameObject menubackground;
    private RectTransform oldrt;
    private RectTransform newrt;
    private float oldx;
 
    private void Awake()
    {
  
    }
    void Start()
    {
        //Fetch the RectTransform from the GameObject
        canvas = gameObject.GetComponent<Canvas>();
        Debug.Log(canvas.enabled);
        //old canvas size
        oldrt = canvas.GetComponent<RectTransform>();
        oldrt.sizeDelta = new Vector2(oldrt.sizeDelta.x, oldrt.sizeDelta.y);
        Debug.Log("oldrt value" + oldrt.sizeDelta.x);
        //transfer result of oldrt to oldx
        oldx = oldrt.sizeDelta.x;
        Debug.Log("oldx" + oldx);
    }
    void Update()
    {
        //getting the rect values and scaling
       AutomaticSetScale();
    }
    private void AutomaticSetScale()
    {
       float newvalue;
    
        newrt = canvas.GetComponent<RectTransform>();
        newrt.sizeDelta = new Vector2(newrt.sizeDelta.x, newrt.sizeDelta.y);
        newvalue = newrt.sizeDelta.x / oldx;
  
        Debug.Log("rt value" + newrt.sizeDelta.x);
        if (newrt.sizeDelta == new Vector2(1920f, 1080f) || newrt.sizeDelta == new Vector2(2560f, 1080f))
        {
            //background native is 4K ( 5120 x 2160) scale it to 0.5f for 1080p
            menubackground.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        }
        else
        {
            //scaling value
            //formula
            /*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*)
            .*/
        
            menubackground.transform.localScale = new Vector3(0.5f * newvalue, 0.5f * newvalue, 0.5f * newvalue);
            Debug.Log("newvalue =" + newvalue);
        }
    }
}