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?
The array works fine though, if I take out the last falling speed line everything works. I got it to work by changing GameObject to Transform... a bit unsure why this worked.
– negative_zero