Instantiation in C#

I am trying to instantiate a GameObject but getting “The prefab you want to instantiate is null.”

It is in the PlantedSeed.cs script. It seems to me that this should create a new instance of the PlantedSeed prefab without having to do anything in editor, but trying to fix it I’ve also tried the variable PlantedSeedPrefab to public, adding this Script to the PlantedSeed prefab, and then dragging the PlantedSeed prefab onto that area in the script (which seems wrong.)

private string variety;
private int germinationTime;
private GameObject plantedSeedPrefab;
private GameObject player;

public void Plant(string vari, int germ)
    Debug.Log("Planting a " + vari);
    player = GameObject.FindWithTag("Player");
    PlantedSeed newSeed = (PlantedSeed) Instantiate(plantedSeedPrefab, player.transform.position + player.transform.forward, Quaternion.identity) as PlantedSeed; = "seed";
    newSeed.variety = vari;

Oh, I also changed the instantiation line so that PlantedSeed was GameObject, but that makes it so newSeed doesn’t have any of the variables like variety (and others I didn’t paste.)

Thanks in advance.

You are not really setting any value to the Game Object (plantedSeedPrefab) you are trying to instantiate.

Remember a prefab is reusable Game Object that you need to have made earlier and you want to make (instances) of.

so make your game object attach any components to it and then save it as a prefab by dragging it into your project hierarchy.

now to load you should make (plantedSeedPrefab) a public variable, and link that saved prefab to your script from the inspector.

Another thing is that you always instantiate prefabs as (GameObjects), if you want to access certain components like (variety) above, you can get using GetComponent like so (Assuming newSeed is now a GameObject):