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

Resolved 2D Sprite & Tilemap localscale resetting after Start()

Discussion in 'Scripting' started by zapman502, Mar 11, 2021.

  1. zapman502

    zapman502

    Joined:
    Apr 13, 2020
    Posts:
    14
    Ok so, in short I'm trying to change the scale of both a singular sprite, and a Grid to scale the child tilemap as well. Before anyone asks "Why do that?" Its because when the application starts, I want the Start() function to scale the assets based on the resolution. I simply want it to work that way, makes it easier for me to manage. If it is impossible for it to work that way I will try another option. Here is my script that I have attached to both objects. I have tried Vector3 at first, so now I'm trying with Vector2. All the examples I've seen use something similar and it works for them but I dont understand why after the Start function they return to normal scale. The debug image I provided shows this. I do not have an animator component on either of these objects as I read that can cause an issue. They are both children of a UI panel with 0 opacity to help with centering the sprite in an odd area on the app.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Scaling : MonoBehaviour
    7. {
    8.     float scaleFactor;
    9.     // Start is called before the first frame update
    10.     public void Start()
    11.     {
    12.         if (Screen.width == 720 && Screen.height == 1280)
    13.         {
    14.             scaleFactor = 1f;
    15.         }
    16.         if (Screen.width == 1080 && Screen.height == 1920)
    17.         {
    18.             scaleFactor = 2f;
    19.             print("Oooga booga"); //Just to make sure it ran...
    20.         }
    21.  
    22.  
    23.         var thisObj = this.transform.localScale; //To spare typing
    24.         thisObj = new Vector2(thisObj.x * scaleFactor, thisObj.y * scaleFactor); //where the scaling is done
    25.         print(thisObj.x);//display new scale.
    26.         print(thisObj.y);
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         var thisObj = this.transform.localScale;
    32.         print(thisObj.x); //Check Scale. CURRENT ISSUE: Scale IMMEDITATELY Resets after Start.
    33.         print(thisObj.y);
    34.     }
    35. }
    36.  
    Cracking my head open over this, no amount of research helped.
     

    Attached Files:

  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    mopthrow and zapman502 like this.
  3. zapman502

    zapman502

    Joined:
    Apr 13, 2020
    Posts:
    14
    Thank you! I didn't know about the terms you linked above, it was a good read. What I ended up doing was making a new line after line 24 and adding: this.transform.localscale = thisObj. That seems to have fixed it.