Search Unity

How to change scale of a GameObject in run-time ?

Discussion in 'Scripting' started by masudias, Oct 27, 2011.

Thread Status:
Not open for further replies.
  1. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    Hello,

    I'm new in Unity. I want to change scale (size i.e. width , height ) during run-time of a gameobject. How can I possibly do that? I've used localscale function it but didn't work properly. May be I didn't use it properly :( . Any help ?

    Thanks in advance,
    _Masudias
     
  2. Julien G.

    Julien G.

    Joined:
    Oct 20, 2011
    Posts:
    25
  3. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    S_Dave and darshan-kirubakaran like this.
  4. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    I want a plane fit in any size of screen .

    I'm not sure but this code may do like this
    previous object is enlarged by 0.1

    I've tried this code :

    But it won't work !
    The Gameobject became very large than it was expected !

    I'm such a noob sire :(
    Help me out !

    Thanks
    _Masudias
     
  5. JannikKunz

    JannikKunz

    Joined:
    Sep 30, 2014
    Posts:
    1
    Use this for once
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.      GameObject rotater;
    7.  
    8.       void Start () {
    9.        rotater = GameObject.Find("Cube");
    10.        }
    11.     public void btnChangeHeight() {
    12.       rotater.gameObject.transform.localScale += newVector3(0,50,0);
    13.     }
    14. }
    15.  
    While button is pressed (repeatButton)
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7.  
    8.         GameObject rotater;
    9.  
    10.         void Start () {
    11.                rotater = GameObject.Find("Cube")
    12.          }
    13.         void OnGUI() {
    14.                 if (GUI.RepeatButton (new Rect (40, 60, 70, 21), "Height")) {
    15.                 rotater.gameObject.transform.localScale += new Vector3(0,1,0);
    16.                 }
    17.          }
    18. }
    19.  
     
    Last edited: Oct 21, 2014
  6. bluelock3

    bluelock3

    Joined:
    May 24, 2015
    Posts:
    2
    i tried your code and everytime it says expressions denotes a type where value was expected
     
  7. LeeTruHee

    LeeTruHee

    Joined:
    Jul 16, 2015
    Posts:
    1
    thank u very much, I like this
     
    DhavalGam likes this.
  8. NiaG-A

    NiaG-A

    Joined:
    Aug 19, 2017
    Posts:
    2
    I have a YouTube video about this
    it only requires a few lines of code
     
  9. danieldesouzamelo07

    danieldesouzamelo07

    Joined:
    Dec 17, 2019
    Posts:
    1
    its unavailable
     
    mSzr likes this.
  10. tomer201120

    tomer201120

    Joined:
    Mar 15, 2020
    Posts:
    3
    tnxxxx
     
  11. levity152

    levity152

    Joined:
    Mar 7, 2021
    Posts:
    2
    Noob here, I think this is the easiest method:

    Define the rigidbody of your object in Start(), like this:
    playerRb = GetComponent<Rigidbody>();

    Then put this in an if loop. Adjust float size as needed:
    playerRb.transform.localScale += new Vector3(0.05f, 0.05f, 0.05f);
     
    SorArc likes this.
  12. Martin_Anderson

    Martin_Anderson

    Joined:
    Oct 4, 2021
    Posts:
    4
    That is definitely not the easiest way, using the rigid body is unnecessary. Instead, you can just write
    transform.localScale += new Vector3(0.05f, 0.05f, 0.05f);
     
    Last edited: Nov 24, 2021
    edhpoisson, Trifinn and Cauleeflower like this.
  13. CasaNu

    CasaNu

    Joined:
    Dec 21, 2021
    Posts:
    15
    i get Non-invocable member 'Vector3' cannot be used like a method.
     
  14. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    You named a variable of your own "Vector3". Don't do that, rename that variable.
     
Thread Status:
Not open for further replies.