How do I change an object's scale in code?

Hi. I am making a game where if the player collides with a certain object they’re scale will change. How can I do this? Here’s how I set up the if statement: Now how can I change the scale of the player within that if statement?

using UnityEngine;
using System.Collections;

public class GetLonger : MonoBehaviour {

	void OnCollisionEnter(Collision col){
		if(col.gameObject.name == "Biggerer"{

		}
	}
}

1 Answer

1

transform.localScale is what you’re looking for, just take the current and add in whatever you want through a Vector3.

using UnityEngine;
using System.Collections;
 
public class GetLonger : MonoBehaviour {
 
    void OnCollisionEnter(Collision col){
        if(col.gameObject.name == "Biggerer"{
            col.gameObject.transform.localScale += new Vector3(1, 0, 1);
        }
    }
}

Thank you so much, but how can I add on to the object's scale? For example if my cube's scale (1, 1, 1) how can I make in that scale + 1 one. So every time the player touches the object it will get longer.

–

What i posted adds onto the current scale, hence += The vector3 describes which axis, x, y and z that you wish to add to and by what amount. if all 1's and you will scale uniformly, etc. you can also use the static [variables on the Vector3 class][1] [1]: http://docs.unity3d.com/ScriptReference/Vector3.html

–