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
2
Question by AbePoppy · Oct 11, 2013 at 04:39 PM · instantiateprefabnullreferenceexceptiondrag

NullReferenceException from a script after linking prefab with drag and drop in inspector. WTF?

Hi guys, sorry to bother you all, I'm quite a noob in Unity but I'm learning. I have this problem: I'm creating a class that for the moment just Instantiates a prefab.

  using UnityEngine;
     using System.Collections;
     
     public class DeckController : MonoBehaviour {
         
         public GameObject testCard;
         
         public GameObject DrawCard()
         {
             Debug.Log("testCard: " + testCard.name);
             
             GameObject temp = (GameObject) Instantiate(testCard);
             
             Debug.Log("tempIsNow: " + temp.name);
             return temp;
         }
     }

I dragged the prefab form the Prefabs folder into the public variable: testCard. As you can see here: alt text

What happens is that I get a runtime exception:

NullReferenceException: Object reference not set to an instance of an object DeckController.DrawCard () (at Assets/Scripts/CardController/DeckController.cs:10)

Row 10 is where there is the first Debug.Log, so it seems that testCard is set to null. I don't understand how this is possible since even in official tutorials this seemed to be the procedure... :( Help please!

immagine.png (89.4 kB)
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 Bunny83 · Oct 11, 2013 at 04:42 PM

Just a guess but:

Add this line at the very beginning of your DrawCard function:

     Debug.Log("GameObject name: " + gameObject.name);

to reveal that this script is attached to another object ;)

Comment
Add comment · Show 3 · 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 AbePoppy · Oct 11, 2013 at 05:19 PM 0
Share

Thanks so much! I solve the problem. In the moment that you supposed that the script was not in the correct position. I understood that the problem was something similar. DrawCard() was called from another script where there was a reference to the Class DeckController ins$$anonymous$$d of a reference to the component DeckController of the game object Deck1. Thanks a lot, you set me on the right road!

avatar image AmazinJacks · Jan 31, 2020 at 11:59 AM 0
Share

I wish there was more info here - I am having the same problem, but I don't fully understand what the solution was.

avatar image Bunny83 AmazinJacks · Jan 31, 2020 at 03:20 PM 0
Share

He has attached the script to more than one object. He did initialize the variable properly on the object he actually cared about. However he had another instance somewhere else where he did not assign the prefab in the inspector. So he did not call his DrawCard method on the instance he originally expected but on that "other" instance. Note that prefabs stored in the project are also "instances" but not in the scene. They exist in memory but don't do anything on their own.


If you have more questions (and also for the future), please do not post questions in comments to other questions / answers. If you have an issue, ask your own question and add your specific details to the description. (we do not own crystal balls to read your $$anonymous$$d. ;) )

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

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

Related Questions

very weird NullReferenceException error causeing one script to break another 2 Answers

Put a prefab in Hierarchy without using Instantiate ? (from code) 2 Answers

Prefab GameObject's can not be made active! 3 Answers

Referencing gameObject from script after Instantiate 0 Answers

Instantiate a prefab through code in C#. 4 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