Need to change a transform.position.y value with a += in C# Close but no cigar!

I’m trying to rework a JS script to C# for practice and have hit a bump I can’t get passed.

I’d like to change this:

void Update () 
{
	if(itemBounceUp == true)
		{
			this.transform.position.y = += bounceSpeed;
		}
}

to C#

If the new value of this.transform.y = (for example), were 5.0f I’d do something like:

transform.position = new Vector3(transform.position.x, 5.0f, transform.position.z);

But not sure how to include a += value (or an -= for that matter).

A bit of guidance would be mighty appreciated, thanks,
B.

transform.position.y += bounceSpeed;

The above should work fine.

Ok… first, Thanks for the help, Garth.

I included a minor typo while screwing around, so I switched it back to your suggestion. But c# doesn’t allow me to directly change a value such asthis.transform.position.y += bounceSpeed;

It requires me to use a temporary variable. One way that seems to work is:

			Vector3 temp = transform.position;
			temp.y += bounceSpeed;
			transform.position = temp;

But I was wondering if there was a way to format it something like this:

transform.position = new Vector3(transform.position.x, transform.y -= bounceSpeed, transform.position.z);
1 Like
transform.position = new Vector3(transform.position.x, transform.y -  bounceSpeed, transform.position.z);

Great, thanks much. And to all who assist the code-impaired. It really makes a big difference that you take the time.

B.

You could also do…

transform.position += new Vector3(0, bounceSpeed, 0);

or

Vector3 bounceSpeed = new Vector3(0, 5, 0);

transform.position += bounceSpeed;

Thanks Kyle…

I’ve been the art/design guy on all my previous projects. The crossover to the code side feels a bit like after NEO took the Red pill and got ripped out of the Matrix.

B.

So for clarification… all the times I’m seeing ‘new’ as in the above, its basically used to create a new value that can’t or shouldn’t be changed directly). Though that’s probably a broad generalization.

Not exactly, you totally can adjust a value directly. What’s really important here is that UnityScript handles a step automatically that C# does not. You can do this, for example:

Vector3 vec = new Vector3(0,5,0);
vec.y += 2;

What you cannot do, however, is this…

transform.position.y += 2;

The reason for this has nothing to do with the “new” keyword - more to do with the way structs work in general. Basically, a struct is (by default) passed around as a value and not a reference. What that means is when you “get” transform.position it’s returning an identical Vector3 to the current position - but not actually returning a reference to the current position. So, if you modify it - it doesn’t automatically apply to the source property. What you’re really asking the compiler to do is this:

Vector3 tempVector = transform.position;
tempVector.y += 2;

As you can see, this would not actually result in transform.position being adjusted at all - only your local variable that was initialized to contain the same value as transform.position. UnityScript automagically handles this for you by making that one line 3 instructions…

transform.position.y += 2; // This actually is telling the compiler to do...

Vector3 tempVector = transform.position;
tempVector.y += 2;
transform.position = tempVector;

C# doesn’t do this automagically for a lot of reasons, but mostly because it obfuscates the behavioral differences between value and reference type variables. UnityScript is all about making things as easy as possible (in terms of less lines of code) - C# is all about making things as clear and predictable as possible while making things easy where it can (in terms of less lines of code).

tl;dr - transform.position gives you a copy of the position, not access to the position itself - modifying that copy would not modify transform.position. UnityScript does some auto-magic to perform the necessary steps for you, C# does not.

Damn Kyle… that was a terrific explanation. Many thanks for the detailed explanation.

Oddly, the depth of your understanding mildly depresses me as to my prospects of becoming anything more useful as a coder than testicles on a Bishop. But I do like a challenge.

Beers on me one day…one Jersey guy to another.

B.

C# has tricks of its own if you know what you’re doin’ :slight_smile:

public static class Helper
{     
    public static void SetPos (this Transform transform, float? x = null, float? y = null, float? z = null) 
    {
        Vector3 position = transform.position;

        if (x.HasValue) position.x = x.Value;
        if (y.HasValue) position.y = y.Value;
        if (z.HasValue) position.z = z.Value;
        transform.position = position;
    }

    public static void AddPos (this Transform transform, float? x = null, float? y = null, float? z = null) 
    {
        Vector3 position = transform.position;

        if (x.HasValue) position.x += x.Value;
        if (y.HasValue) position.y += y.Value;
        if (z.HasValue) position.z += z.Value;
        transform.position = position;
    }
}

...
transform.SetPos(x: 5);
transform.AddPos(y:5, z:-2);
1 Like

By the way, what npfs3000 posted was a couple of extension methods - an incredibly powerful tool in C# that I highly suggest looking into as you progress your knowledge. I wouldn’t get too bogged down trying to learn them until you know the language better, but when the time comes you can do magic with them :smile:

1 Like