Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity'. Consider storing the value in a temporary variable

I get this error even though i’ve assigned variables (v1 and v2). This is driving me insane. Any help is appreciated.

using UnityEngine;
using System.Collections;

public class BulletController : MonoBehaviour {


	public float speed;
	public PlayerController player;
	public BulletController largeBullet;
	private float spawnChooser;
	public Transform firePoint;
	public GameObject targetBullet;


	public float v1;
	public float v2;




	

	// Use this for initialization
	void Start () {
		player = FindObjectOfType<PlayerController> ();
		largeBullet = FindObjectOfType<BulletController> ();
		if (player.transform.localScale.x < 0) {
						speed = -speed;
						transform.localScale = new Vector3 (-1f, 1f, 1f);
				} else if (player.transform.localScale.x > 0)
						transform.localScale = new Vector3 (1f, 1f, 1f);
	}
	
	// Update is called once per frame
	void Update () {
		rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
	}
	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.tag == "Enemy") {
						Destroy (gameObject);
				}
	} 
	public void BulletTermination()
	{
		Destroy (this.gameObject); 

	}
	public void SpawnChooser()
	{
		spawnChooser = Random.value;
		if (spawnChooser < 0.2) 
		{
			Instantiate (largeBullet, firePoint.position, firePoint.rotation );
			targetBullet = gameObject;
			rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y = v1 ); //error here


			
		}
		
		if (spawnChooser < 0.4 && spawnChooser > 0.2)
		{
			Instantiate (largeBullet, firePoint.position, firePoint.rotation );
			targetBullet = gameObject;
			rigidbody2D.velocity = new Vector2(speed,rigidbody2D.velocity.y = v2); //error here


		}

			if (spawnChooser < 0.6 && spawnChooser > 0.4) {
						Instantiate (largeBullet, firePoint.position, firePoint.rotation);
			targetBullet = gameObject;
			rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y = -v2); //error here

				}
		if (spawnChooser < 0.8 && spawnChooser > 0.6)
		{
			
			Instantiate (largeBullet, firePoint.position, firePoint.rotation  );
			targetBullet = gameObject;
			rigidbody2D.velocity = new Vector2(speed,rigidbody2D.velocity.y); 


		}
		if (spawnChooser < 1 && spawnChooser > 0.8)
		{
			Instantiate (largeBullet, firePoint.position, firePoint.rotation  );
			targetBullet = gameObject;
			rigidbody2D.velocity = new Vector2(speed,rigidbody2D.velocity.y   = -v1); //error here


			
		}
	}
}

Let’s dissect one of those lines which are similar to one another:

rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y = v1 );

Ok, so the expression that fails is this:

rigidbody2D.velocity.y = v1

Why? It’s because rigidbody2D.velocity is a property returning a value type (A Vector2, which is a struct, which is a value type).

Value types are passed as copies, not references. That means that you’ll get a copy of its velocity. Setting someCopy.y doesn’t make sense, because it’ll have no effect. You’d at best modify memory that nothing uses, so the whole idea is pointless. What you should consider is doing what the error tell you.

Store the value in a temporary variable (and the rest it doesn’t tell you: modify the temporary variable, and assign it back).

Also note that your expression rigidbody2D.velocity.y = v1 doesn’t make much sense because it would be the same as just v1, if velocity wasn’t a property.

Vector2 temporaryVariable = rigidbody2D.velocity;
temporaryVariable.y = temporaryVariable.y = v1; // Doesn't make much sense, does it?
rigidbody2D.velocity = new Vector2(speed, temporaryVariable.y);

The above could be written as

rigidbody2D.velocity = new Vector2(speed, v1); // What do you need velocity.y for anyway?

btw, error lines are 90,78
74,79
66,78
65,79
15,34