scale a gameobject

Hello :slight_smile:
is it possible to scale a gameObject?
This One:

var bildObject : GameObject = new GameObject.CreatePrimitive(PrimitiveType.Plane);
bildObject.transform.position = Vector3(0, 0.5, 0);
bildObject.transform.Rotate(-90,0,0);

Or should I take the Mesh Object?
TIA

Yes,

for scaling relative to the GameObject it’s parented to.

or

for scaling in absolute terms relative to the world.

Be warned though, this affects all of that objects children, making moving much harder to calculate. If it’s scaled to 2.0x, you only have to move child objects half the amount to go full distance.

2 Likes

thank you :wink:

It works fine :slight_smile:

bildObject.transform.localScale.x *= bildGroesse;
bildObject.transform.localScale.z *= bildGroesse;

Is it possible to short it?
I try instead of x and z the one and the this :roll:
Im really a noob at this point.

1 Like

I’m trying to do the EXACT same thing, but for some reason I keep getting an error from Unity. I have an orthographic camera and am trying to scale a game object based on its Z position:

void scaleAvatar(){
		zscale = (fullZspace - transform.position.z) / fullZspace;
		currentScale = ((maxScale - minScale) * zscale) + minScale;
		//print(transform.position.z +" "+ currentScale);
		transform.localScale.x = currentScale;
		transform.localScale.y = currentScale;
		transform.localScale.z = currentScale;
	}

But I keep getting this error:

Assets/Scripts/AvatarAnimator.cs(123,27): error CS1612: Cannot modify the return value of `UnityEngine.Transform.localScale' because it is not a variable

I am pulling my hair out because I know this should be very simple and I’m sure there’s something basic I have overlooked.

1 Like

That is an issue with Properties and Syntax.

You can do a few things:

transform.localScale[0] = currentScale;
transform.localScale[1] = currentScale;
transform.localScale[2] = currentScale;

or

transform.localScale = new Vector3(currentScale, currentScale, currentScale);

or

Vector3 scale = transform.localScale;
scale.x = currentScale;
scale.y = currentScale;
scale.z = currentScale;

All of these should provide a working alternative(although at least one may lose it’s reference, as they are untested I can’t recall).

I find that syntax issue with accessing subobjects of properties to be one of the most annoying syntax nuances of C#.

It’s a struct, so you should use:

transform.localScale = new Vector3(currentScale, currentScale, currentScale);
or
transform.localScale = new Vector3(transform.localScale.x * bildGroesse, transform.localScale.y, Vector3(transform.localScale.z * bildGroesse);

That won’t work for the same reason as above (error CS1612).

That won’t work unless you put “transform.localScale = scale;” after.

This is not an “issue” with C#, or “subobjects of properties” in general. It is due to the behaviour of structs, and for as “annoying” as it is, structs are a very worthwhile addition to any language.

Read here for a better explanation as to why this happens:
http://www.eggheadcafe.com/software/aspnet/32457954/cannot-modify-the-return-value-because-it-is-not-a-variable.aspx

Thanks, that cleared up that issue so I can move on to the next one. :slight_smile: