After instantiating an instance of a prefab and changing the value of a variable in a script attached to the prefab, that value is lost

I’m making a simple sidescrolling 2D game where the player is either facing right or left (based on whether they have moved left or moved right most recently). The player should be able to shoot a missile in front of them and have it travel in the direction they are facing. I do this using this code in the Update function of the script controlling the player:

//Shoot
	if((Time.time - lastShot) >= shotWait) {
		canShoot = true;
	}
	if(Input.GetKey(KeyCode.E) && canShoot) {

		var control: PlayerShotControl;
		if(direction == "Right") {
			Debug.Log("Shooting, direction is right");
			Instantiate(shot, transform.position + Vector3(0.75f, 0.0f, 0.0f), Quaternion.Euler(0.0f, 0.0f, 270.0f));
			control = shot.GetComponent(PlayerShotControl);
			control.direction = "Right"; 
			Debug.Log("After assignment, control.direction is " + control.direction);
		} else if (direction == "Left") {
			Debug.Log("Shooting, direction is left");
			Instantiate(shot, transform.position + Vector3(-0.75f, 0.0f, 0.0f), Quaternion.Euler(0.0f, 0.0f, 270.0f));
			control = shot.GetComponent(PlayerShotControl);
			control.direction = "Left"; 
		Debug.Log("After assignment, control.direction is " + control.direction);
		} else Debug.Log("Spaceship direction = " + direction);
            canShoot = false;
		lastShot = Time.time;

“shot” is the prefab for the missile I want to shoot. The Update function for the script controlling the shot is this:

function Update () {
	if(direction == "Right") {
		Debug.Log("Shot moving. Direction is right");
		transform.position.x += moveSpeed;
	} else if (direction == "Left") {
		Debug.Log("Shot moving. Direction is left");
		transform.position.x -= moveSpeed;
	} else Debug.Log("Shot Direction is " + direction);

When I actually have the player shoot, it works fine except for the first shot after I have changed direction. The character is originally acing right. If I shoot without changing direction, the missile fires to the right like it should. Once I turn left and shoot again, the shot is instantiated at the correct location for shooting left(Notice in the player’s Update function the shot’s x value is different depending on whether it is shooting right or left), however, it goes right. If I fire again while still facing left, it works like it should. It continues to work correctly except that the first shot after changing directions is fired in the wrong direction.

The Debug statements my code show that the player’s direction is always correct, and it always sends the correct direction to the shot. So after turning left, the debug shows “Shooting, direction is left” and “After assignment, control.direction is Left”. However, the debug in the shot’s Update says that its value for direction is “Right” even though it was just set to “Left” and never changed. So after turning left, shooting, and getting the debug messages from the player above, the Shot still reports “Shot moving. Direction is right”.

Can anyone offer some insight as to what is going on here? Any help would be greatly appreciated. Also if I missed some important part of my script that should be here, let me know and I will post it. Thanks!

Recently I also faced the problem by mistake. I was accessing the prefab script not that instance script.

GameObject obj = (GameObject) Instantiate(shot, transform.position + Vector3(0.75f, 0.0f, 0.0f), Quaternion.Euler(0.0f, 0.0f, 270.0f));

control = obj.GetComponent(PlayerShotControl);

control.direction = “Right”;

In the second line of your code which i pasted, reference of that instance (means “obj”) must be there instead of “shot”. I think this would help you.