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 /
This question was closed Feb 20, 2013 at 11:11 PM by Geo.Ego for the following reason:

I won't be able to pursue troubleshooting this problem as we are no longer using this code.

avatar image
0
Question by Geo.Ego · Feb 15, 2013 at 06:41 PM · instantiateprefabcoroutinenullreferenceexception

Object reference to set to instance on an instantiated object

I have a C# script where I am instantiating a prefab inside of a coroutine. When I attempt to access that prefab from another coroutine or anywhere else in the code, I am getting a null reference exception; however, I know that the object exists, as I can see it on-screen, and I can reference it later in the same coroutine that instantiates it. Here is the relevant code:

 private Vector3 originalPosition = new Vector3 (0, 0, 0);
 public GameObject masterPrefab;
 private GameObject instantiatedPrefab;
 
 void Start ()
 {
      instantiatedPrefab = null;
 }
 
 private IEnumerator SpawnPrefab ()
 {
      if (instantiatedPrefab == null)
      {
           instantiatedPrefab = (GameObject)Instantiate (masterPrefab, originalPosition, new Quaternion (0, 0, 0, 0)) as GameObject;
      }
 
      print ("Prefab created at " + instantiatedPrefab.transform.position.ToString ());
 }

If I call StartCoroutine (SpawnPrefab ()), the prefab is instantiated. I can see it on the screen and it behaves as expected. The print statement accesses the instantiated prefab's position and prints it correctly. However, if I attempt to acces the instantiated prefab anywhere else -- in Update, or another coroutine, for example -- I get the null reference exception. So the following will throw that error:

 private IEnumerator DoSomethingWithPrefab ()
 {
      print ("Do something with prefab located at " + instantiatedPrefab.transform.position.ToString ());
 }

As well as this:

 void Update ()
 {
      print ("Prefab position: " + instantiatedPrefab.transform.position.ToString ());
 }

I'm stumped. I figured if I successfully instantiated the object, I would be able to manipulate it within the script, but I'm clearly missing something.

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 FLASHDENMARK · Feb 15, 2013 at 08:29 PM 0
Share

Had this problem as well. What fixed my problem was by changing the type of the instantiated object to Transform ins$$anonymous$$d of GameObject:

private Transform instantiatedPrefab;

1 Reply

  • Sort: 
avatar image
0

Answer by robertbu · Feb 15, 2013 at 07:42 PM

I tried to repo your problem and failed, so something more is going on. It occurred to me that you would have this problem if this script was attached to multiple objects, and you had masterPrefab initialized in only one of them. Here is the script I used to attempt to repro the problem:

 public class Debug2 : MonoBehaviour {
 
 private Vector3 originalPosition = new Vector3 (0, 0, 0);
 public GameObject masterPrefab;
 private GameObject instantiatedPrefab;
  
 void Start ()
 {
      instantiatedPrefab = null;
     StartCoroutine (SpawnPrefab ());
     if (instantiatedPrefab == null)    
             Debug.Log ("null");
     else
             Debug.Log ("created");
 }
  
 private IEnumerator SpawnPrefab ()
 {
      if (instantiatedPrefab == null)
      {
           instantiatedPrefab = (GameObject)Instantiate (masterPrefab, originalPosition, new Quaternion (0, 0, 0, 0)) as GameObject;
           yield return 0;
      }
  
      print ("Prefab created at " + instantiatedPrefab.transform.position.ToString ());
 }
     
 void Update ()
 {
     if (instantiatedPrefab != null)
      print ("Prefab position: " + instantiatedPrefab.transform.position.ToString ());
 }    
 }
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Referencing gameObject from script after Instantiate 0 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

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

Accessing a prefab after instantiating results as null 1 Answer

Having multiple objects fire prefabs in different times C# 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