Set public variables on a script on prefab

Hello, I have a player controller instantiating a bullet prefab.

nextFire = Time.time + fireRate;
GameObject obj =(GameObject)Instantiate(shot,shotSpawn.position,shotSpawn.rotation);
BasicBulletController test = obj.GetComponents<BasicBulletController>();
Debug.Log (test);

and it’s giving me the error "Assets/Scripts/PlayerController.cs(41,47): error CS0029: Cannot implicitly convert type BasicBulletController[]' to BasicBulletController’ "

I’m not sure, what I’m doing wrong or what the square brackets in the error mean.
I know there are a lot of similar question flying around, but I’d really appreciate some help learning what I’m missing here, and not just how to rush through this hurdle.

You use GetComponents while you probably want to use GetComponent

The square brackets indicate an array. A native array is simply a type followed by square brackets:

// C#
int[] someIntArray = new int[5];

GetComponents (note the “s” at the end) returns all instances of the given class type in an array. If there is only one instance on that GameObject it would return an array with only one element.

btw: There’s an easy way to simplify your above code snippet. When using Instantiate you can pass a reference to a component on the prefab instead of a reference to the GameObject of the prefab. Instantiate does the same, it will clone the whole GameObject with everything, but it will return a reference to that component you passed in. That way you don’t need to use GetComponent at all. Example:

// changed the old "shot" variable type to BasicBulletController
// when doing this you will need to assign your prefab again to that variable
//public GameObject shot;           // old
public BasicBulletController shot;  // new

// when instantiating you get back a reference to your component, so you have to change the cast:
BasicBulletController test = (BasicBulletController)Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
Debug.Log (test);