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 Gumiibear · Nov 06, 2016 at 09:17 AM · unity 5gameobjectcomponent

Storing component of instance in an array

I'm trying to store the scripts from several different instances of the base class "AeroplaneControls" within an array. So that I can later use this array to quicker access all the different methods contained in the subclasses. However when I do this I get the following error "Object reference not set to an instance of an object"

  GameObject[] Vehicles;
  AeroplaneControls[] Controls;
  
  Vehicles = GameObject.FindGameObjectsWithTag ("Vehicle");
  Controls = new AeroplaneControls[Vehicles.Length];
  for(int i = 0; i < Vehicles.Length; i++){
       Controls[i] = Vehicles[i].GetComponent<AeroplaneControls> ();
  }

It is not refeering to my gameObject "Vehicles" since I can do other stuff with it like find it's position, so the problem is with my "AeroplaneControls" script array.

Comment
Add comment · Show 3
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 Cherno · Nov 06, 2016 at 09:22 AM 0
Share

There is no mention of an "enemy" variable anywhere in your code.

Check if all elements of Vehicles actually have the AeroplaneControls component. Use Debug.Log lines to print the result to console.

avatar image Gumiibear Cherno · Nov 06, 2016 at 11:05 AM 0
Share

It was a typo, I meant "Vehicles". And yes, every Vehicle has a component which is a subclass of "AeroplaneControls" (AeroplaneControls is and abstract class).

I did try debug.log before the last part: Controls[i] = Vehicles[i].GetComponent ();

And my result was: For the gameobject

 Aeroplane2 (UnityEngine.GameObject)
 UnityEngine.Debug:Log(Object)

And for the script:

 Aeroplane2 (AeroplaneScript2)
 UnityEngine.Debug:Log(Object)

So it's refeering to the correct gameobject, and to the same gameobjects script that i want to access. So i don't understand why the error occurs when both are correct.

avatar image Gumiibear · Nov 06, 2016 at 10:11 PM 0
Share

Don't know if it makes any differance, but it worked before putting the scripts into an array. And now when I go back to doing without array it doesn't work anymore.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by daniel-eherbert · Nov 07, 2016 at 02:59 AM

Edit: brain fart...

Your loop is likely failing after it processes all the items due to an incorrect loop limit.

This code:

 for(int i = 0; i < Vehicles.Length; i++) {
     Controls[i] = Vehicles[i].GetComponent<AeroplaneControls> ();
 }

...should probably be (note the - 1):

 for(int i = 0; i < Vehicles.Length - 1; i++) {
     Controls[i] = Vehicles[i].GetComponent<AeroplaneControls> ();
 }

Array indices start with 0, so if you iterate while i < Vehicles.Length the loop will run 1 time too many (0, 1, 2, 3, ... 20 = 21 iterations)

Comment
Add comment · Show 2 · 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 Gumiibear · Nov 07, 2016 at 01:15 PM 0
Share

That is unfortunately not the case. i < 5 for example will iterate 4 times, since the 5th time it's equal and not less than 4. And since there in this example 5 items in the list I get the positions (0,1, 2, 3, 4), which are every one I need. Furthermore if that was the issue then I would get an error saying "array index out of range" since nothing exists in position 5.

$$anonymous$$y problem is most likely with the component itself, but I have no clue since whatever I do now it still doesn't work, even though it worked before

avatar image daniel-eherbert Gumiibear · Nov 07, 2016 at 09:52 PM 0
Share

You're absolutely correct! Quite the slip of $$anonymous$$d there - apologies for that.

Are you able to use a debugger and step through the loop to see which part of it is tripping it up?

I see in the comment above that you've tried debug.log and appear to be getting values for each iteration...

Are you able to share more of the script?

What line exactly does the error occur on?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to dynamically attach a script to a GameObject during runtime? 1 Answer

Access water4 scripts via my script? 1 Answer

Unity 5.6 error UnityEngine.Component' does not contain a definition for `materials' and no extension method `materials' of type `UnityEngine.Component' could be found. Are you missing an assembly reference? 1 Answer

Calling an Audio Source on one game object from a script on another game object..? 1 Answer

Please help my head is burning from this problem : i have multiple gameobject , same script 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