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 kvdleij · Jul 23, 2012 at 07:09 PM · instantiateprefabnullargumentexception

ArgumentException: The prefab you want to instantiate is null.

Hey guys,

I'm running into a problem. I have a script which instantiates a prefab, like this:

 public class SegmentScript : MonoBehaviour {
 
  public GameObject bar_piece;
  private int start_height;
  private int end_height;
  private int score;
  private GameObject[] pieces;
  private Vector3 start_location;
  
  // Use this for initialization
  public void Start () {
  
  pieces = new GameObject[10];
  
  AddPieces(start_height, end_height);
  }
  
  public void AddPieces(int start, int end){
  
  for(int i = start;i<end;i++){
  Vector3 piece_location = start_location;
  piece_location.y = i;
  
  GameObject new_piece = Instantiate(bar_piece, piece_location, Quaternion.Euler(0,0,0)) as GameObject;
  pieces[i] = new_piece;
  }
  }
 }

Now, when I select the script in the inspector, I can drag and drop a prefab to the bar_piece slot. During runtime, I add this script through another script using the AddComponent function.

But somehow the prefab slot for bar_piece is empty again and I get the ArgumentException.

The weirdest part is that another script uses the exact same method to instantiate an object, but that works fine.

Comment
Add comment
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

1 Reply

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

Answer by ScroodgeM · Jul 23, 2012 at 07:12 PM

AddComponent will not copy linked objects to this component. it just adds an empty component. if you want to save link you should:

  1. add component in editor window

  2. attach all links you need to this component

  3. save gameobject with component to a prefab

  4. instantiate this prefab in runtime

second solution:

right after AddComponent attach all objects also in script like this:

addedComponent.someLinkToPrefab = this.someLinkToPrefab;
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 kvdleij · Jul 24, 2012 at 08:44 AM 0
Share

That worked, thanks! Too bad it doesn't work like I thought it would do, this seems kind of a workaround.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Some Prefabs are not Loading 1 Answer

Initializing Prefab returns NullPointerException 1 Answer

ArgumentException: The prefab you want to instantiate is null. 1 Answer

Instantiating prefab from another script. Error, prefab is null. 1 Answer

the prefab you want to instantiate is null 1 Answer


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