- Home /
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);
//THIS IS WHERE I AM
player = GameObject.FindWithTag("Player");
PlantedSeed newSeed = (PlantedSeed) Instantiate(plantedSeedPrefab, player.transform.position + player.transform.forward, Quaternion.identity) as PlantedSeed;
newSeed.name = "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 haven't initialized plantedSeedPrefab. I suspect it is because you don't understand prefabs. Reference page:
http://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html
Answer by OmarAlhaddad · Mar 09, 2013 at 07:37 PM
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):
newSeed.GetComponent<PlantedSeed>.vareity;
Goodluck
Thank you, I've made plantedSeedPrefab public, and put the Prefab from the Hierarchy onto the place in the script, which is actually on the Prefab itself. Is this weird? Seems like loop.
I changed the Instantiation line to:
GameObject newSeed = (GameObject)Instantiate(plantedSeedPrefab, player.transform.position + player.transform.forward, Quaternion.identity) as GameObject;
and am still getting "The Prefab you want to Instantiate is Null." As was pointed out by robertbu, I evidently am not getting something about instantiating prefabs, I thought what I am doing in the above line is making it not null by creating it with values. I've read the page linked to, and many others, but there seems to be a fundamental disconnect.
Thanks again.
Your answer
Follow this Question
Related Questions
instantiating vertically 2 Answers
How to make TCG Deck (Instatiate based on Prefab) 1 Answer
How do I make prefab follow a gameobject?(C#) 1 Answer
Instantiate already connected players. 0 Answers