Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 nggb · Jan 28, 2014 at 05:43 PM · arraygetcomponentanother script

Array index out of range when access from another scrip

I'm trying to access an array from another script in C# but it always give me index out of range error.

script1.cs (attached to mainCamera named MC)

 public int[] testArray = {1,2,3,4};


script2.cs (attached to one of the prefabs)

 public int[] NEWtestArray;
 
 NEWtestArray[0] = MC.GetComponent<script1>().testArray[0]; //gives an index out of range
 
 //tried to debug
 Debug.log(MC.GetComponent<script1>().testArray[0]); //same error
 
 //but if I do this
 NEWtestArray = MC.GetComponent<script1>().testArray;
 Debug.log(NEWtestArray[0]); //this works and would output 1. 

So, how can I access individual elements without copying the entire array? I'm using the main array in the camera to pull large amount of static game data from an xml file. It just doesn't make sense to copy this entire array over to each of the objects in the scene that need individual data from this array.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Marionette · Jan 28, 2014 at 09:14 PM

make sure that the array is actually being created properly in script1 first. I would do something like this to test it:

public int[] testArray = null;

then in awake/enable/start (wherever you need to initialize)..

declaring it static might also help ;)

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
avatar image
0

Answer by Xtro · Jan 28, 2014 at 08:04 PM

Don't set the default values when defining the members.

Write this into the Start event

 testArray = {1,2,3,4};
Comment
Add comment · Show 1 · 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
avatar image Xtro · Jan 30, 2014 at 09:02 PM 0
Share

Can you please mark the correct answer if you fixed that problem?

avatar image
0

Answer by nggb · Jan 29, 2014 at 07:51 AM

Thanks for all the answers.

First, I want to clarify in my original post, my declaration of the array is wrong. In my code, I actually declare the array as:

 public int[] testArray;
 
 Start (){
 
 testArray = new int[] {1,2,3,4};
 }
 

Second, I found out why I was getting the error. While both of part of array code is initialize in the Start() function of script1 and script2, for some reasons script2 was always initialized before script1, therefore, when script2 look for the array, it did not exists. During my search today, I found out that the Awake() function always initialize before the Start() function. So I moved my array code in script1 from Start() to Awake() and now everything is working as it should.

Comment
Add comment · Show 1 · 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
avatar image Xtro · Jan 29, 2014 at 02:52 PM 0
Share

That was my answer :)

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

20 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

Related Questions

How to FindChild RectTransform and access its Text 2 Answers

Need an advise, how to find all objects with a specific name and a specific script? 2 Answers

Take array and waypoints from parent spawnner 2 Answers

Create array of scripts 1 Answer

Obtaining an array of positions from an array of gameobjects 2 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