I try to do CancelInvoke inside Update (to somehow have control over Invoking), and its still executing. Its like it wont cancell invoked function. I can still hear the audio and so on.
using UnityEngine;
using Random = UnityEngine.Random;
public class TelephoneBooth : MonoBehaviour
{
public AudioClip ringSound;
public AudioSource source;
public bool _isOpen;
void Start()
{
//Open();
}
private void Update()
{
if (_isOpen && !IsInvoking("Open"))
{
Invoke ("Open", 3);
Debug.Log("Invoked");
}
if (!_isOpen && IsInvoking("Open"))
{
CancelInvoke ("Open");
Debug.Log("CANCEL");
}
}
void Open()
{
InvokeRepeating("Ring", 0, 3.0f);
InvokeRepeating("ShakeThePhone", 0, 0.1f);
}
void Ring()
{
source.PlayOneShot(ringSound);
}
void ShakeThePhone()
{
transform.localScale = new Vector2(1f, Random.Range(0.1f, 0.3f)+1f );
}
}