Search Unity

[SteamVR] Referencing a Raycasthit variable across scripts

Discussion in 'Scripting' started by averma21, Feb 9, 2018.

  1. averma21

    averma21

    Joined:
    Feb 9, 2018
    Posts:
    1
    Hello,

    I am developing a game using the SteamVR plugin, and am currently attempting to have my laserPointer script interact with a buttonPanel script. The button panel uses hit.collider.tag to determine which button has been hit by the laserPointer's Raycasthit variable. However, I get a NullReferenceException at the line where hit.collider.tag lies.
    Additionally, I used the
    laserPointer script found here. I haven't changed the code in this script, however, I am getting an error on the line where the script is supposed to get and then return the index of the controller. Is there any way I can communicate between multiple scripts to have these two scripts interact? I have already tried using static variables, getting the script component from the GameObject that has the script. Is there anything else I can do?

    Thanks!


    Code (CSharp):
    1. //laserPointer
    2.  
    3. private SteamVR_Controller.Device Controller
    4.     {
    5.         get { return SteamVR_Controller.Input((int)trackedObj.index); }
    6.     }
    7. void Update()
    8.     {
    9.         if (Controller.GetPress (SteamVR_Controller.ButtonMask.Touchpad)) {
    10.             RaycastHit hit;
    11.  
    12.             if (Physics.Raycast (trackedObj.transform.position, transform.forward, out hit, 100)) {
    13.                 hitPoint = hit.point;
    14.                 ShowLaser (hit);
    15.             }
    16.         }
    17.  
    18.         else {
    19.             laser.SetActive (false);
    20.         }
    21.            
    22.     }
    23.  
    Code (CSharp):
    1. //buttonPanel
    2. public RaycastHit hitt;
    3. public Vector3 hitPoint;
    4.  
    5. void Start () {
    6.         scene = SceneManager.GetActiveScene();
    7.  
    8.         hitt = LaserPointer.hit;
    9.         hitPoint = LaserPointer.hitPoint;
    10. }
    11.  
    12. void Update () {
    13.         for (int i = 0; i < buttons.Length; i++) { //goes through array of button tags
    14. //THE LINE BELOW IS WHERE THE NULLREFERENCEEXCEPTION IS HAPPENING
    15.             if (hitt.collider.tag == buttons[i] || buttonHit == true) //if the laser hits any of the buttons
    16.             {
    17.  
    18. ...
    19.  
    20. }
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Well one problem I see is the with your connecting buttonPanel.hitt with LaserPoint.hit

    I assume somewhere in your LaserPointer script we can't see there is a line that looks like this:
    Code (CSharp):
    1. public RaycastHit hit;
    This Raycast hit is never being initialized and thats why you are getting a null exception. The actual variable that is doing the job in LaserPointer here:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Controller.GetPress (SteamVR_Controller.ButtonMask.Touchpad)) {
    4.             RaycastHit hit;
    5.  
    Is a local variable that is being created and thrown away inside of LaserPointer's Update() method. To properly connect the two scripts you would need to have a global public RaycastHit that is used in LaserPointer's Update, then your button script could access it and see it.

    However, this will still lead you to a second problem. Updates across various scripts are not guaranteed to be called in any particular order. So your button's Update might access LaserPointer's RaycastHit before it actually does the collisions checking. One way to fix it is to reverse how your connecting them. Have an array of button's in your LaserPointer script that you link up in the Editor with all your UI buttons. Then your laser pointer can check if it hit any of the buttons and call that particular button's script to do whatever its suppose to do.