How do I change a variable of an object that I created through instantiate (C#)

I know others have asked this, but I tried their “correct” answers and they aren’t working for me. I have a line of code that calls a function to drop a random item selected from an array every second:

InvokeRepeating ("DropCollectible", .1f, 1.0f);

The DropCollectible function looks like this:

public void DropCollectible()
	randomnumber = Random.Range (0, numSelectors);
	GameObject testvar = Instantiate(selectorArr[randomnumber], transform.position, transform.rotation) as GameObject;

	testvar.GetComponent<NonPlayerCollectible>().fallingspeed = -2;

Everything worked fine until I added that last line of code trying to change the falling speed of the items that are generated. The items that are instantiated definitely have the “NonPlayerCollectible” script attached to them, which is a child class of “NonPlayer” which has a public int “fallingspeed” variable. I know all of that is hooked up fine.

What I’m trying to do longterm is make the falling speed of the items random, but at the moment I can’t even get it working with the speed of -2. I don’t get any compile errors, but when I go to run I get the message:

Object reference not set to an instance of an object

I’m not really sure what I’m doing wrong here. Help?

I’m thinking the message means that when it tries to instantiate selectorArr[randomnumber], the value of randomnumber is maybe out of the range of your array. An array with 10 items cannot get myArary[10] since the array index starts at zero and ends at nine. Maybe try:
Random.Range (0, numSelectors-1);

Edit: Actually I’m changing my mind. The message makes me think you might not have filled your array, or correctly linked/set some value in your script’s Inspector window.