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 TurboHermit · Dec 11, 2012 at 02:55 PM · arraylistfor

What am I overlooking?

Quick question: What am I overlooking in this piece of code?

     for(var n = 0; n < Blocks.Count; n++)
     {
         BlockScripts[n] = Blocks[n].GetComponent(BlockScript);
     }

Blocks is an List of GameObjects and BlockScripts is an array of BlockScripts.

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 CodeMasterMike · Dec 11, 2012 at 03:14 PM 1
Share

What is not happening? Or what is happening that shouldn't be happening?

$$anonymous$$aybe its better to set it like this ins$$anonymous$$d:

    BlockScripts[n] = Blocks[n].GetComponent<BlockScript>();

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TurboHermit · Dec 11, 2012 at 05:00 PM

Wow. I found the problem. I haven't set the length of the BlockScripts array. Sorry for wasting your time =P

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
1

Answer by Landern · Dec 11, 2012 at 03:17 PM

GetComponent returns a typeof Component, using the generic version of GetComponent, the return type is the script type you are looking for. Or you could cast it(second example)

 for(var n = 0; n < Blocks.Count; n++)
 {
     BlockScripts[n] = Blocks[n].GetComponent.<BlockScript>(); //js
     //BlockScripts[n] = Blocks[n].GetComponent<BlockScript>(); //c#
 }

Without using GetComponent.< T >();

 for(var n = 0; n < Blocks.Count; n++)
 {
     BlockScripts[n] = (BlockScript)Blocks[n].GetComponent(BlockScript);
 }

This is a guess, you didn't post an error or exception, if you did you should post it.

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 TurboHermit · Dec 11, 2012 at 04:58 PM 0
Share

Oh yeah, sorry forget to post what's wrong =P I'm getting a null object reference error. There's a total of 5 objects in the Blocks array, ALL with the BlockScript component.

avatar image TurboHermit · Dec 11, 2012 at 04:58 PM 0
Share

And I only get one error.

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

12 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

Related Questions

Generic list of objects, then distance to those objects? (js) 1 Answer

For all elements above a certain index 1 Answer

A node in a childnode? 1 Answer

Using List.Count on Unity iPhone 2 Answers

Getting Vector3 from random item in List 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