Adam
Adam

Reputation: 174

Flashlight toggle script initiates flash on startup

On startup the flashlight toggle script mode toggles ON/OFF, I'm not sure how to patch this.

I believe it's coming from IEnumerator Start() but, I've tried changing the yield return new WaitForSeconds to 0 but that didn't change a thing.

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using Vuforia;

public class FlashlightAPI : MonoBehaviour
{
    IEnumerator Start()
    {

        yield return new WaitForSeconds(0);

        hasTorch = CameraDevice.Instance.SetFlashTorchMode(true);
        yield return new WaitForSeconds(0.000f);
        CameraDevice.Instance.SetFlashTorchMode(false);
    }

    bool torchState = true, hasTouch = false;
    public bool hasTorch;

    public FlashlightAPI(bool torchState, bool hasTorch)
    {
        this.torchState = torchState;
        this.hasTorch = hasTorch;
    }
} 

Upvotes: 0

Views: 352

Answers (1)

Zero is too long

Wait makes Unity wait until the next frame after the condition has passed. In this case, it waits for the first frame after at least 0 seconds have elapsed, meaning that it waits 1 frame.

If you want it to happen instantly, you need to remove the Waits entirely.

void Start()
{
    hasTorch = CameraDevice.Instance.SetFlashTorchMode(true);
    CameraDevice.Instance.SetFlashTorchMode(false);
}

However as you're fiddling with an external device (the phone's camera), this may still cause the light to flash and you should refer to the documentation to resolve the issue. As CameraDevice is not a Unity class I cannot do this for you.

Upvotes: 1

Related Questions