Error trying to make a character controller crouch and prone.

Hey, guys.

Could anyone help me with the below error, I receive it when I use the script below to try and make my character crouch and go prone.

Assets/CrouchAndProne.cs(72,25): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition’. Consider storing the value in a temporary variable

Thanks.

using UnityEngine;
using System.Collections;

public class CrouchAndProne : MonoBehaviour 
{
	
	private Vector3 initialPosition;
	private float initialHeight;
	private float crouchingHeight = 0.0f;
	private float proneHeight = 0.0f;
	private Vector3 initialCenter;
	private Vector3 crouchingCenter;
	private Vector3 proneCenter;
	private float TimingDelta = 0.0f;
	bool isStanding = false;
	bool isCrouching = false;
	bool isProne = false;
	public float holdTime = 0.0f;
	public float goProne = 0.08f;
	public bool canGoProne = false;
	
	CharacterController characterController;
	
	Transform mainCam;
	float defCamPos;
	float crouchCamPos;
	float proneCamHeight;
	float curCamPos;
	float speed;

	
	// Use this for initialization
	void Start () 
	{
		characterController = GetComponent<CharacterController>();
		
		initialHeight = characterController.height;
		crouchingHeight = initialHeight - 0.5f;
		proneHeight = initialHeight - 1.4f;
		initialCenter = characterController.center;
		crouchingCenter = initialCenter + Vector3.down * 0.25f;
		proneCenter = initialCenter + Vector3.down * 0.6f;
		isStanding = true;
		isCrouching = false;
		isProne = false;
		canGoProne = true;
	}
	
	
	void Awake()
	{
		defCamPos = mainCam.localPosition.y;
	}

	
	// Update is called once per frame
	void Update () 
	{
		if(isStanding)
		{
			curCamPos = defCamPos;
		}
		if(isCrouching)
		{
			curCamPos = crouchCamPos;
		}
		if(isProne)
		{
			curCamPos = proneCamHeight;
		}
		
		mainCam.localPosition.y = Mathf.Lerp(mainCam.localPosition.y, curCamPos, speed * Time.deltaTime);
		
		//-------------------------------------------------------------------------------------
		
		if(Input.GetButtonDown("Crouch") == true  isStanding == true)
		{
			characterController.height = crouchingHeight;
			characterController.center = crouchingCenter;
			Crouch ();
		}
		else if(Input.GetButtonDown("Crouch") == true  isCrouching == true)
		{
			characterController.height = initialHeight;
			characterController.center = initialCenter;
			Stand ();
		}
		else if(Input.GetButtonDown("Crouch") == true  isProne == true)
		{
			characterController.height = initialHeight;
			characterController.center = initialCenter;
			canGoProne = false;
			Stand ();
		}
		if(Input.GetButton("Crouch") == true  canGoProne == true)
		{
			TimingDelta = 0.1f * Time.deltaTime;
			holdTime += TimingDelta;
			if(holdTime >= goProne)
			{
				characterController.height = proneHeight;
				characterController.center = proneCenter;
				Prone ();
			}
		}
		if(Input.GetButtonUp("Crouch"))
		{
			holdTime = 0.0f;
			if(canGoProne == false)
			{
				canGoProne = true;
			}
		}
	}
	
		
	
	void Stand()
	{
		isStanding = true;
		isCrouching = false;
		isProne = false;
	}
	
	void Crouch ()
	{
		isStanding = false;
		isCrouching = true;
		isProne = false;
	}
	
	void Prone ()
	{
		isStanding = false;
		isCrouching = false;
		isProne = true;
	}
	
}

You can’t change the y-axis of localPosition directly. You’ll need to create new Vector3 and assign that to localPosition, instead. Unfortunately it’s an easy mistake to make.