Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrAdventure · Mar 09, 2013 at 07:33 AM · c#instantiateprefab

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 09, 2013 at 07:36 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrAdventure · Mar 10, 2013 at 05:59 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Save gameobjects prefabs from scene A to a list, then instantiate those game objects to another scene. 0 Answers

Instantiate already connected players. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges