Question by
soccer1mt · Aug 26, 2016 at 10:39 AM ·
event-handlingindexoutofrangeexceptionindexing-array
How to GetComponent for SteamVR_TrackedObj when script not attached to controller
How do you properly GetComponent of a SteamVR_trackedObj when the script is not going to be attached to the controller in the camera rig prefab that comes in the steamVR plugin. In the code I have below, I keep getting Array Index out of range error. Any insight or guidance would be very much appreciated.
`using UnityEngine; using System.Collections;
public class MovementOverall : MonoBehaviour {
//This script is attached to the player game object. It tells the NavMeshAgent component to set its destination for the flag when the
// player is alive. It also has the player rotate in the direction where the laser from the right controller is pointing. Still working on
// what to do with the player once he is killed.
public SteamVR_TrackedObject trackedObj;
[HideInInspector] public Transform flag;
public static UnityEngine.NavMeshAgent nav;
private bool IsRightDown = false;
private Vector3 controllerVector;
void Awake()
{
flag = GameObject.FindGameObjectWithTag("Flag").transform;
nav = GetComponent<UnityEngine.NavMeshAgent>();
trackedObj = trackedObj.GetComponent<SteamVR_TrackedObject>();
}
void Update()
{
if (TimeDestroy.isPlayerAlive)
{
NavWhenAlive();
}
}
void FixedUpdate()
{
IsTriggerPressed();
FlagReposition();
}
void playerRotator()
{
Vector3 lookPos = LaserPointer.laserhitpoint - transform.position;
lookPos.y = 0f;
Quaternion rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 10f);
}
void NavWhenAlive()
{
nav.SetDestination(flag.position);
nav.updateRotation = false;
playerRotator();
if (Vector3.Distance(this.gameObject.transform.position, flag.position) <= .001)
{
nav.Stop();
}
}
void IsTriggerPressed()
{
SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackedObj.index);
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("Trigger is down");
IsRightDown = true;
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("Trigger is up");
IsRightDown = false;
}
}
void FlagReposition()
{
if (IsRightDown)
{
controllerVector.x = trackedObj.transform.position.x;
controllerVector.z = trackedObj.transform.position.z;
controllerVector.y = .55f;
transform.position = controllerVector;
}
}
} '
Comment
I'm following. (Though this hasn't been answered after a year). Did you solve this? I also noticed that
GetComponent<S$$anonymous$$mVR_TrackedObject> ();
works ONLY if the script is attached to the prefab. When you try to get the object using
trackedObject = GameObject.Find ("Controller (left)").GetComponent<S$$anonymous$$mVR_TrackedObject> ();
from another object, it doesnt work. I also tried to use:
public S$$anonymous$$mVR_TrackedObject trackedObjectR;
public S$$anonymous$$mVR_TrackedObject trackedObjectL;
void Start () {
trackedObjectR = GetComponent<S$$anonymous$$mVR_TrackedObject> ();
trackedObjectL = GetComponent<S$$anonymous$$mVR_TrackedObject> ();
}
Didn't work either.