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 /
avatar image
0
Question by Thrasill · Dec 08, 2015 at 12:02 PM · transformarrayarraysindexout of range

Array of transform index out of range error

What is wrong in here? This is the second time I want to ask this because the first time it just started to work and I didn't need to ask anymore. But now, again it doesn't work and I can't figure out what it is. I get index out of range error and the array is empty whilst moments ago it worked just fine and got all the objects in the arrays.

using UnityEngine; using System.Collections;

public class PipeTopBottomDistance : MonoBehaviour { public GameObject[] pipes = new GameObject[8]; public Vector3[] positions;

 public Transform[] bottomTransforms = new Transform[8];
 public Transform[] topTransforms = new Transform[8];
 public float verticalPipeDistance = 3.08f;
 public float pipePressOn = 3.08f, pipePressOff;
 public float pressSpeed;
 
 void Start ()
 {
    pipes = GameObject.FindGameObjectsWithTag("PipeHolder");

    

    for(int i = 0; i < 8; i++)
     {
         positions[i] = pipes[i].transform.position;
         bottomTransforms[i] = pipes[i].transform.GetChild(0);
         topTransforms[i] = pipes[i].transform.GetChild(1);
     }
    
 }
 
 void Update ()
 {   
     
     
 }

}

Comment
Add comment · Show 2
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 Thrasill · Dec 07, 2015 at 03:42 PM 0
Share

Now after playing a bit with the script.. like commenting out some lines to see if I can isolate the problem.. or deleting the "new Transforms[8]" when declaring the transforms and still failing to work.. it now works again and I brought the code back to be literally exactly the same as the one I pasted here and it works.. what am I missing? I fear it will happen again and I want to know exactly what the problem is..

avatar image Thrasill · Dec 07, 2015 at 03:44 PM 0
Share

And now I just copied and pasted the script I put here to be sure I'm not missing anything and it still works... Why it didn't work earlier.. what would the explanation be?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FlyingHighUp · Dec 08, 2015 at 08:39 PM

It could be from multiple places here.

The for loop you have requires there be >= 8 GameObjects in your scene with the tag "PipeHolder". If GameObject.FindGameObjectsWithTag finds less than 8, pipes[i] will throw a exception.

You'll also get the exception if positions[], bottomTransforms[] or topTransforms[] have less than 8 elements. Also, because they're being serialized to the inspector - you need to make sure they have 8 elements in the inspector.

In addition to this, each found pipe Gameobject needs to have two children. If not you'll get a Unity exception.

I'm not exactly sure what you're trying to accomplish with your script, but personally I would write

 using System.Collections.Generic;
 
 public class YourClass : MonoBehaviour 
 {
     [HideInInspector] public List<Vector3> Positions = new List<Vector3>();
     [HideInInspector] public List<Transform> TopTransforms =  new List<Transform>();
     [HideInInspector] public List<Transform> BottomTransforms = new List<Transform>();
 
     void Start()
     {
         GameObject[] pipes = GameObject.FindGameObjectsWithTag ("PipeHolder");
         foreach (GameObject pipe in pipes) 
         {
             Positions.Add (pipe.transform.position);
                     if( pipe.transform.childCount >= 1)
                   TopTransforms.Add (pipe.transform.GetChild (0));
                     if( pipe.transform.childCount >= 2)
                   BottomTransforms.Add (pipe.transform.GetChild (1));
         }
     }
 }

This version works with any number of pipes, and any number of Pipe children.

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I check a List[0], when it's nothing? 1 Answer

"Array index is out of range" for any index value, when array.length>index 2 Answers

How do I check the distance between multiples objects in an array from the player? 4 Answers

Changing the material of all children, then change back to old one 1 Answer

Find index number in an Array of a transform by it's name 1 Answer


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