Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sergiobd · Oct 20, 2017 at 05:09 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to have one script to call upon levels please help!! 2 Answers

IndexOutOfRangeException in Animation 1 Answer

Unity C# array index is out of range 1 Answer

How to call function on AssetDatabase.importPackageCompleted 1 Answer

Minesweeper UI Issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges