It is very important that you
do not use static variables
in this case.
You will be pleased to know that the solution is simple. Have a script calledAttachToDoor
public void AttachToDoor()
{
private bool isOpen;
public float doorSpeed;
etc etc
public void OpenCloseDoor()
{
your code to open/close a door
}
}
Drag AttachToDoorto your door. Notification OpenCloseDoor()marked public.
Next, ANOTHER, SEPARATE script, AttachToButton
public void AttachToButton()
{
public AttachToDoor amazaingDoorScript;
etc etc
void Update()
{
if (Input.GetButton("Fire1"))
if (DoPlayerLookAtButton() && isAnimationReadyToPlay)
amazaingDoorScript.OpenCloseDoor();
}
}
AttachToButton "".
"amazaingDoorScript".
AttachToDoor amazaingDoorScript.
... , .
AttachToButton .
script, , , :
public AttachToDoor amazaingDoorScript; ...
Invoke("test",Random.Range(5f,10f)); ...
private void test()
{
amazaingDoorScript.OpenCloseDoor();
Invoke("test",Random.Range(5f,10f));
}
Unity, , . !
, , UnityEvent.
Unity, UnityEvent. UnityEvent Unity.
: fooobar.com/questions/1627429/...
public void AttachToButton()
{
public UnityEvent buttonClicked;
void Update()
{
if (Input.GetButton("Fire1"))
if (DoPlayerLookAtButton() && isAimationReadyToPlay)
{
Debug.Log("Button pressed!");
if (buttonClicked!=null) buttonClicked.Invoke();
}
}
}
. , , , Log "!".
, .
, , AttachToButton.
. . buttonClicked. , ... OpenCloseDoor.
, . ! ?