Interact with Objects with scripts

Hi,

i want to open a door and i have 3 scripts.


First Script (Select) Assigned to GameCamera:

void Update () {

	Ray ray = transform.camera.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2,0));

	if ( Physics.Raycast(ray, out hit,4) && hit.collider.gameObject.GetComponent<Interact>() != null)
	{
		hit.collider.gameObject.GetComponent<Interact>().OnLookEnter();	
	}
}

Second Script (Interact) Assigned to any Object i want to interact with (in this case the door:

void Update ()
{
canInteract = false;
renderer.material.shader = origShader;
}

public void OnLookEnter()
{		
	canInteract = true;
	renderer.material.shader = Shader.Find("Self-Illumin/Bumped Diffuse");
	
}

Third Script (OpenDoor) Assigned to the Door to open

void Update ()
{

	canInteract = this.GetComponent<Interact>().canInteract;
	
	if (open == true)
	{
		var target = Quaternion.Euler(0,DoorOpenAngle,0);
		door.localRotation = Quaternion.Slerp(door.localRotation,target,Time.deltaTime * smooth);		
	}

	if (open == false)
	{
		var target1 = Quaternion.Euler(0,DoorCloseAngle,0);
		door.localRotation = Quaternion.Slerp(door.localRotation,target1,Time.deltaTime * smooth);		
	}

	if (canInteract)
	{
	
		if (Input.GetMouseButtonDown(0))
		{
			if (open == false)
			{		
				(open) = true;
				
			}
			else
			{
				(open) = false;
				
				
			}
			
			this.PlayOpenDoorSound();
		}		
	}	
}

so if i walk to the door, the “canInteract” from the Interact script changes right, but the door “canInteract” which references to the “canInteract” from the Interact-Sript in the openDoor script does not Change.
What did i wrong ?

Thx!

that’s right, but i have to, cause when i leave the object (door) and onLookEnter is not called, it has to be false, imo. it works well with the shader, when i focuse the door it flashes and when i leave it gets the Default shader back.

What you are trying is already answered here:

link text

You can talk to other scripts primarily by:
A: Making a gameobject in Script A, and placing the Object you want to interact with in it, you can get the Children by GetComponentInChildren.

B: Use the above method, which you already are, but I dont know if canInteract is a public field, hence you might need to declare it a “static” so that its visible outside the instantiation of an object. If you dont get the value via a “public” field like canInteract, try ‘public static bool canInteract’.

You cant access a field “canInteract” without the object being already created, that too via a Updating function. Update function gets called after Awake and Start, which would mean that you are trying to access a field without its object actually existing which would lead to a null error.

Hope that helps.