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

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 :frowning: . Any help ?

Thanks in advance,
_Masudias

juste check localScale doc :wink:

Julien G.

5 Likes

Indeed, that should do it.

Masudias, do you know what to do with the given code?

2 Likes

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 :frowning:
Help me out !

Thanks
_Masudias

Use this for once

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
     GameObject rotater;

      void Start () {
       rotater = GameObject.Find("Cube");
       }
    public void btnChangeHeight() {
      rotater.gameObject.transform.localScale += newVector3(0,50,0);
    }
}

While button is pressed (repeatButton)

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
 

        GameObject rotater;

        void Start () {
               rotater = GameObject.Find("Cube")
         }
        void OnGUI() {
                if (GUI.RepeatButton (new Rect (40, 60, 70, 21), "Height")) {
                rotater.gameObject.transform.localScale += new Vector3(0,1,0);
                }
         }
}
7 Likes

i tried your code and everytime it says expressions denotes a type where value was expected

thank u very much, I like this

1 Like

I have a YouTube video about this

it only requires a few lines of code

its unavailable

1 Like

tnxxxx

Noob here, I think this is the easiest method:

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

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

1 Like

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

3 Likes

i get Non-invocable member ‘Vector3’ cannot be used like a method.

You named a variable of your own “Vector3”. Don’t do that, rename that variable.