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 beeboks · Mar 02, 2015 at 12:01 PM · instantiategetcomponent

Getting a component of an instantiated object returns an error

Hi, I'm trying to get a component of an instantiated object in C# but it keeps returning errors whenever I try it. I have a prefab with a C# script component called MenuButton and I'm using this code to instantiate it and get the menu button component:

 GameObject b = Instantiate(optionPrefab, transform.position, Quaternion.identity) as GameObject;
 MenuButton b_btn = b.GetComponent<MenuButton>();

but I keep getting the same error:

 NullReferenceException: Object reference not set to an instance of an object
 CreateStartOptions.Start () (at Assets/CreateStartOptions.cs:16)

So to me it looks like the C# script or gameobject itself hasn't yet been registered during the instantiate call and it doesn't find the components of the object until the next frame. This same code always worked for me in UnityScript but since I've moved over to C# it's given me the same error consistently.

How can I insure that I can get the component upon instantiation?

Comment
Add comment · Show 2
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 AlwaysSunny · Mar 02, 2015 at 07:55 AM 0
Share

try an as $$anonymous$$enuButton after the method invocation?

 $$anonymous$$enuButton b_btn = b.GetComponent<$$anonymous$$enuButton>() as $$anonymous$$enuButton;

avatar image Bonfire-Boy · Mar 02, 2015 at 12:31 PM 0
Share

@beeboks Which line is line 16?

The code you've shown us wouldn't throw a NRE just because it can't find the $$anonymous$$enuButton component (it would just set b_btn to null in that case). It looks to me more likely that b itself is null.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by POiD · Mar 02, 2015 at 02:12 PM

I do this quite often in my scripts using similar logic as you do, I just cast to a gameobject [(GameObject)] instead of using "as", but I doubt that's the issue.

Are you sure that optionPrefab is assigned to a GameObject variable in your script where you are trying to instantiate it? As in did you make the variable public and/or use [SerializeField] and then drag a copy of the Prefab into the variable spot in Unity Inspector?

 [SerializeField] GameObject optionPrefab; 

The other option is are you sure of the spelling of your Script attached to your prefab? As in you are accessing a script "MenuButton"?

Have you tried with a standard component added to the prefab, such as a BoxCollider for example?

 GameObject newObject = (GameObject)Instantiate(optionPrefab);
 BoxCollider ourBox = newObject.GetComponent<BoxCollider>();
Comment
Add comment · Show 2 · 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 beeboks · Mar 03, 2015 at 05:33 PM 0
Share

Yeah, I had all of the hookups in. I didn't use [SerializeField]. What does that do?

I got it working now by casting the Instantiated object as a Transform ins$$anonymous$$d of a GameObject.

 Transform newObject = (Transform)Instantiate(optionPrefab);
 $$anonymous$$enuBtn m_btn = newObject.gameObject.GetComponent<$$anonymous$$enuBtn>();

Do you know why that would work but GameObject wouldn't?

avatar image POiD · Mar 04, 2015 at 06:34 PM 0
Share

Hi Beeboks, I have no idea. In my code I'm always casting to a GameObject and it works. Did you try casting to a GameObject rather than use "as" ?

SerializeField: http://docs.unity3d.com/ScriptReference/SerializeField.html

From my understanding this allows you to set the field via the Editor, however doesn't require it to be Public, so that other scripts couldn't just access the variable. Came across it in a tutorial somewhere.

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

How do I acces a specific GameObject within the terrain???? 1 Answer

instantiate prefab and link 3rd party gameobject script 1 Answer

Instantiate Prefabs. Errors. 2 Answers

Only change a variable on the instaniated object not the prefab. 0 Answers

Play iTween on instantiated prefab 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