Get Vector3 From Another Script

I’m trying to make the player character turn, based on the way that it moves in another GameObject (The movement is controlled by a parent object and the turning is controlled by a child object). Right now my code looks like:

using UnityEngine;
using System.Collections;

public class turn : MonoBehaviour {

public float turnSpeed = 6.0F;

private int moving = 0;	
public Transform camera;
public GameObject player;
private Vector3 cameraDirection =;
private float movex;
private float movey;
private Vector3 turning;
public Vector3 

	void FixedUpdate () {
		if (Input.GetAxis("Horizontal")==0F && Input.GetAxis("Vertical")==0F)
			 moving = 0;
			 moving = 1;
		if (moving == 0)
			cameraDirection = camera.transform.forward;
	float turn = turnSpeed * Time.deltaTime;
	var script = player.GetComponent("Movement");
	Vector3 turnDirection = script.GetComponent("moveDirection") as Vector3;
	turning = Vector3.RotateTowards(transform.forward, turnDirection, turn, 0.0F);
	transform.rotation = Quaternion.LookRotation(turning);

and it gives me the error: “The ‘as’ operator cannot be used with a non-nullable value type ‘UnityEngine.Vector3’”. How can I fix this or get the Vector3 another way?

As is a wonder type cast in that if it can no resolve then you can check for null, this means it can not be a value type(a struct, like a Vector3 is) unless you use Nullable, but that is a waste in this case. The problem you may have is if it can’t find it it will throw with a general type cast, but you can change line 29 to:

Vector3 turnDirection = (Vector3)script.GetComponent("moveDirection");

You may want to also use the generic version rather then that string version of GetComponent.

Vector3 turnDirection = (Vector3)script.GetComponent<moveDirection>();

I also doubt that moveDirection is a Vector3 type and is really a script/class you have already written with a field or property with the Vector3 on it, if that is the case.

moveDirection md = (Vector3)script.GetComponent<moveDirection>();
Vector3 moveDirection = md.whateverContainsTheValueYouWantToGrab;