Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

HTC Vive - Reference lost on play

Discussion in 'Scripting' started by Shorkieboyo, Jun 26, 2017.

  1. Shorkieboyo

    Shorkieboyo

    Joined:
    Mar 10, 2017
    Posts:
    14
    Hello, I'm trying to figure out the touchpad for the VR Controllers and it's driving me nuts, yet again...

    Here's the code to get input:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Valve.VR;
    4. using UnityEngine;
    5.  
    6. public class Metronome : MonoBehaviour
    7. {
    8.  
    9.  
    10.     //Controller stuff
    11.     public SteamVR_Controller.Device device;
    12.     public SteamVR_TrackedObject controller;
    13.     Vector2 touchpad;
    14.     public int BeatsPerMinute = 1;
    15.     public int firstBeat;
    16.     //public bool turnedOn;
    17.     public AudioSource soundSource;
    18.     public AudioSource sourceTick;
    19.     public AudioClip metronomeTick1;
    20.     public AudioClip firstTIck;
    21.     public GameObject light1;
    22.     public GameObject light2;
    23.     public GameObject light3;
    24.     public GameObject light4;
    25.  
    26.     private float _t;
    27.  
    28.     private void Awake()
    29.     {
    30.  
    31.         //CS
    32.         controller = gameObject.GetComponent<SteamVR_TrackedObject>();
    33.      
    34.     }
    35.  
    36.     void Start()
    37.     {
    38.         soundSource.gameObject.GetComponent<AudioSource>();
    39.         firstBeat = 0;
    40.        
    41.  
    42.     }
    43.  
    44.     void OnEnable()
    45.     {
    46.         _t = 0f;
    47.     }
    48.  
    49.     private void FixedUpdate()
    50.     {
    51.         this.tuneBPM();
    52.     }
    53.  
    54.     void Update()
    55.     {
    56.    
    57.         float dur = 60f / this.BeatsPerMinute;
    58.         _t += Time.deltaTime;
    59.         while (_t >= dur)
    60.         {
    61.             _t -= dur;
    62.             this.CodeToRun();
    63.         }
    64.  
    65.  
    66.         this.LightSwitch();
    67.      
    68.  
    69.     }
    70.  
    71.  
    72.  
    73.     public void CodeToRun()
    74.     {
    75.         firstBeat += 1;
    76.  
    77.         if (firstBeat > 3)
    78.         {
    79.             firstBeat = 0;
    80.         }
    81.  
    82.         if (firstBeat > 0)
    83.         {
    84.             soundSource.Play();
    85.             Debug.Log("Metronome BPM");
    86.  
    87.         }
    88.  
    89.         if (firstBeat == 0)
    90.         {
    91.             sourceTick.Play();
    92.             Debug.Log("First tick");
    93.  
    94.         }
    95.  
    96.     }
    97.  
    98.     public void LightSwitch()
    99.     {
    100.         if (firstBeat == 0)
    101.         { light1.SetActive(true); }
    102.         else
    103.         { light1.SetActive(false); }
    104.  
    105.         if (firstBeat == 1)
    106.         { light2.SetActive(true); }
    107.         else
    108.         { light2.SetActive(false); }
    109.  
    110.         if (firstBeat == 2)
    111.         { light3.SetActive(true); }
    112.         else
    113.         { light3.SetActive(false); }
    114.  
    115.         if (firstBeat == 3)
    116.         { light4.SetActive(true); }
    117.         else
    118.         { light4.SetActive(false); }
    119.     }
    120.  
    121.     public void tuneBPM()
    122.     {
    123.         if (controller == null)
    124.         {
    125.             Debug.Log("Controller not found");
    126.  
    127.         }
    128.  
    129.         device = SteamVR_Controller.Input((int)controller.index);
    130.  
    131.        
    132.         //If finger is on touchpad
    133.         if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
    134.         {
    135.             Debug.Log("Touchpad Click");
    136.             //Read the touchpad values
    137.             touchpad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
    138.         }
    139.  
    140.     }
    141.  
    142.  
    143. }
    But when I press on "Play" the object "controller" is loosing the reference, even though I dragged and dropped the controller before hand. But when the game is paused and I re-drag and drop it in, then I cllck on the play button so the game continuous and it works fine, I get the Debug.Log and everything.
    Any ideas?

    Thanks!
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Whatever you do to get the controller in the inspector, you overwrite it when you do this in Awake:
    Code (CSharp):
    1. controller = gameObject.GetComponent<SteamVR_TrackedObject>();
    Is that intentional?
     
    Shorkieboyo and BlackPete like this.
  3. Shorkieboyo

    Shorkieboyo

    Joined:
    Mar 10, 2017
    Posts:
    14
    No it wasn't, thanks!
     
    LiterallyJeff likes this.