- Home /
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.
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>();
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
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.
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.
Your answer
Follow this Question
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