Tried to convert from JS to C#

Hi all,

I downloaded a free app called Floatate from the app store which is in JS but I’d rather have it in C#, as that’s what I’m learning at the moment. I’m generally new to coding anyways and I have one question about this particular error I’m getting:

Assets/Standard Assets/Scripts/BaloonScript.cs(35,27): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable

And here is the code, I’m going to keep the copyright message in it:

/**************************************
 * Copyright (c) 2012, Timothy Thomas *
 *        Some rights reserved.       *
 **************************************/

using UnityEngine;
using System.Collections;

public class BackgroundSpeeds : MonoBehaviour {
	
	public float bobSpeed = 3.0f;  //Bob speed
	public float bobHeight = 0.5f; //Bob height
	public float bobOffset = 0.0f;
	
	public float PrimaryRot = 80.0f;  //First axies degrees per second
	public float SecondaryRot = 40.0f; //Second axies degrees per second
	public float TertiaryRot = 20.0f;  //Third axies degrees per second
	
	private float bottom;
	
	//@script AddComponentMenu("Perflexive Media/Floatate")
	
	void Awake () {
		if (bobSpeed < 0) {
			Debug.LogWarning("Negative object bobSpeed value! May result in undesired behavior. Continuing anyway.", gameObject);
		}
		
		if (bobHeight < 0) {
			Debug.LogWarning("Negative object bobHeight value! May result in undesired behavior. Continuing anyway.", gameObject);
		}
		
		bottom = transform.position.y;
	}
	
	void Update () {
		//transform.Rotate(Vector3(0, PrimaryRot, 0) * Time.deltaTime, Space.World);
		//transform.Rotate(Vector3(SecondaryRot, 0, 0) * Time.deltaTime, Space.Self);
		//transform.Rotate(Vector3(0, 0, TertiaryRot) * Time.deltaTime, Space.Self);
		
		transform.position.y = bottom + (((Mathf.Cos((Time.time + bobOffset) * bobSpeed) + 1) / 2 ) * bobHeight);
	}
}

I commented out the rotate functions because I don’t need it at the moment.

It’s never a good idea to change separate axis - values from vector3 position, rotation.
Instead, cache the value you want to change, change the cached version, then assign it back.

Here’s an example.

Vector3 pos = transform.position;
pos.y += 3f * Time.deltaTime;
transform.position = pos;

Literally took me over an hour to get it right, but I finally got it, thanks for the tip!

The code now looks like this:

public class BalloonScript : MonoBehaviour {

	public float bobSpeed = 3.0f;  //Bob speed
	public float bobHeight = 0.5f; //Bob height
	public float bobOffset = 0.0f;
	
	public float PrimaryRot = 80.0f;  //First axies degrees per second
	public float SecondaryRot = 40.0f; //Second axies degrees per second
	public float TertiaryRot = 20.0f;  //Third axies degrees per second
	private float pos;
	private float bottom;
	
	void Awake () {
		if (bobSpeed < 0) {
			Debug.LogWarning("Negative object bobSpeed value! May result in undesired behavior. Continuing anyway.", gameObject);
		}
		
		if (bobHeight < 0) {
			Debug.LogWarning("Negative object bobHeight value! May result in undesired behavior. Continuing anyway.", gameObject);
		}
		bottom = transform.position.y;
		
	}
	
	void Update () {
		//transform.Rotate(Vector3(0, PrimaryRot, 0) * Time.deltaTime, Space.World);
		//transform.Rotate(Vector3(SecondaryRot, 0, 0) * Time.deltaTime, Space.Self);
		//transform.Rotate(Vector3(0, 0, TertiaryRot) * Time.deltaTime, Space.Self);
		pos = bottom + (((Mathf.Cos((Time.time + bobOffset) * bobSpeed) + 1) / 2 ) * bobHeight);

		Vector2 newPos = new Vector2(transform.position.x, pos);
		transform.position = newPos;
		
		
		
	}
}

Do you think there is a cleaner way for me to code that or is this ok?