- Home /
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!
From what I see, you set your canInteract variable back to false in the Update function. So it might be that it gets reset back to false immediately after being set to true.
Answer by UndyingSpite · Nov 04, 2013 at 09:57 PM
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.
Answer by Starwalker · Nov 04, 2013 at 10:12 PM
What you are trying is already answered here:
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.
I dont't get it. I try the example in the link (static variable) i set the field to static but has no effect.
The field "canInteract" in the script "Interact" changes when i leave/pass the door.
But "canInteract" in the script "OpenDoor" will not effected.
when i disable the "canInteract = false" in the Interact script, it works, but only once. of course it will never Change back to false.
In your Update () , you make the canInteract = false once every frame, hence when your OnLookEnter makes it true, its true for ONLY those frames...
if this is a time line, it will look like this:
FFFFFFFFTTTTTTTTFFFFFFFFFFFFFFF ... Frames
The T, or True states only happens for a few frames, and it sets it back to default, False, since Update() changes it back to false.
$$anonymous$$eaning, you should not update the bool inside an update function or change your monitoring mechanism.
In order to solve your problem, make the bool true via a On$$anonymous$$ouseOver function, not an update, and move your canInteract on this function. On$$anonymous$$ouseOver is only called if the object gets $$anonymous$$ouseOver'ed. Now once your interaction with the door is done, set this value back to false, else you wont be able to open it again as default.
you can use this: canInteract = !canInteract on LateUpdate. LateUpdate gets called once after Update()
On series of events: Awake() -> Start() -> Update() [every frame] -> LateUpdate() [Everyframe + 1], FixedFrame() [for Physics]
Your answer
Follow this Question
Related Questions
Accessing variables from another object? 1 Answer
Scripts on imported objects do not work? 1 Answer
Can I Access Objects or Scripts in an Additive Level? 2 Answers
Converting object into script 1 Answer
Accessing other objects 2 Answers