• 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 MattMeer · Aug 04, 2013 at 10:25 PM · instantiateprefabnullreferenceexception

instantiate prefab object to existing object

Hi,

I am trying to instantiate a prefab object, which is a simple Gameobject with a plane (including mesh renderer, mesh filter ofcourse) to the position of an already existing gameobject in the scene. The prefab object to Instantiate doesn't exist in the scene yet, and I have trouble letting unity know which object to instantiate.

It gives me the error: NullReferenceException UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:72) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:82)

Now I know that a NullReferenceException basically means that Unity can't find the object I want to Instantiate.

Here is my code: ( I tried many variations of this)

var GraphicNew : GameObject;

 var Graphic : GameObject;
 Graphic = GameObject.Find("/Graphic");   //the prefab object to instantiate which doesn't exist in the scene, but is in the project files.
 
 function Start () {
 
 
     //attach graphic object to the object of this script
     GraphicNew = Instantiate(Graphic, transform.position, transform.rotation);
     
     GraphicNew.transform.parent = gameObject.transform; //attaches to object
 
 }

Am I missing something? Any help would be appreciated.

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 cdrandin · Aug 04, 2013 at 11:37 PM

GameObject.Find, if you read carefully in the docs (http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html) Finds the game object with the name attached to it. Most likely your object is not called "/Graphic". It seems you are trying to access a folder and instantiate an object from the folder which can't be done this way, or maybe at all, but doubt.

Also, put GameObject.Find in the Start() function, it may actually just work, but if that doesn't then either put the appropriate name or drag and drop using the inspector.

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 MattMeer · Aug 05, 2013 at 12:26 PM 0
Share

Well, I could not get GameObject.Find to work. Your last suggestion however did the trick: I just drag and dropped the correct gameobject to the inspector view to the slot of the Graphic variable. Thanks :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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 avatar image avatar image avatar image

Related Questions

Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers

Accessing a prefab after instantiating results as null 1 Answer

Help how to reference prefab for instantiating between loads. 0 Answers

initial velocity for instantiated ragdoll prefab 1 Answer

NullReferenceException from a script after linking prefab with drag and drop in inspector. WTF? 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