Search Unity

Scale gameobject

Discussion in 'Scripting' started by sonys222, Sep 18, 2015.

  1. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    Hi. I have a question. How to scale gameobject in my 2D game ?
     
  2. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    934
    Something like this should work:

    Code (CSharp):
    1. public GameObject SomeObject;
    2. float NewScaleX = 0.5f;
    3. float NewScaleY = 2.0f;
    4.  
    5. SomeObject.transform.localScale = new Vector2(NewScaleX, NewScaleY);
     
    sonys222 likes this.
  3. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    hmm but I want scale objects to fit on any resolution phone. ( Scale with screen size). Sorry for the vague question. :oops:
     
  4. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    934
    How about this, then?:

    Code (CSharp):
    1. public GameObject SomeObject;
    2. float NewScale1 = 0.2f;
    3. float NewScale2 = 0.4f;
    4. int ScreenWidth = Screen.width;
    5. int ScreenHeight = Screen.height;
    6.  
    7. void Update () {
    8.     if (Input.GetKeyDown(KeyCode.DownArrow)) {
    9.         SomeObject.transform.localScale = new Vector2(
    10.         NewScale1*ScreenWidth, NewScale1*ScreenHeight);
    11.     }
    12.     if (Input.GetKeyDown(KeyCode.UpArrow)) {
    13.         SomeObject.transform.localScale = new Vector2(
    14.         NewScale2*ScreenWidth, NewScale2*ScreenHeight);
    15.     }
    16. }
    17.  
     
    Last edited: Sep 19, 2015
    sonys222 likes this.
  5. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    It does not work because I need the scripts on the phone, so that game objects scale all in accordance with the size of the screen.
     
  6. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    934
    What do you mean by that? Are you trying to run it on iOS?
    It should get the resolution of the device that it runs on.
     
  7. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    I try to run it on Android, but i really don't know how to rescale game objects to work on any size phone. I made a 2D game.
     
  8. Michael-N3rkmind

    Michael-N3rkmind

    Joined:
    Aug 16, 2015
    Posts:
    20
    Is your Camera Orthographic?
     
  9. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
  10. Michael-N3rkmind

    Michael-N3rkmind

    Joined:
    Aug 16, 2015
    Posts:
    20
    Drop this line on your camera object my friend :) It will do the trick

    Code (CSharp):
    1. GetComponent<Camera>().orthographicSize = (20.0f / Screen.width * Screen.height / 2.0f);