Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by rob11 · May 20, 2019 at 11:34 PM · nullreferenceexceptioninstantiate prefabprefabutility

Prefabutility.instantiateprefab returns null SOMETIMES.

Is there any reasons instantiateprefab would return null in some rare case in my room generator script ? It was working perfectly fine with Instantiate() but since I wanted to keep the prefab connection, I swapped everything to prefabutility.instantiateprefab(). It works as before, but sometimes, the instantiateprefab would return null. I tried to find a similar case but nothing would solve my problem.

  private void SpawningTheRoom()
     {
         if (roomManager.MaxRooms > roomManager.RoomCount && roomManager.RoomGrid[spawnerX, spawnerZ] == GridInfo.NA)
         {
             SeeNewRoomRequirements();
             spawnedARoom = true;
             if (mustHaveWestE)
             {
                 if (mustHaveNorthE)
                 {
                     if (mustHaveEastE)
                     {
                         if (mustHaveSouthE)
                         {
                             room = PrefabUtility.InstantiatePrefab(roomEEEE.gameObject) as GameObject;
                             if (!room)
                             {
                                 print("no ee");
                             }
                             room.transform.position = transform.position;                                                       
                         }
                         else
                         {
                             index = Random.Range(0, roomsWNEE.Length);
                             room = PrefabUtility.InstantiatePrefab(roomsWNEE[index].gameObject) as GameObject;
                             if (!room)
                             {
                                 print(index);
                             }
                             room.transform.position = transform.position;                            
                         }
                     }

So here

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by rob11 · May 21, 2019 at 01:02 AM

Okay, after reviewing this problem for another hour, I found something interesting. The null reference exception happens when a room tries to instantiate a room from the same prefab as its prefab. Can

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 FabDynamic · Oct 17, 2019 at 05:56 PM 0
Share

Well I don't have anything to add yet but I'm having the exact same problem: In a dungeon generator a room is trying to instantiate a prefab from the prefab it comes from and sometimes gets null. In my case if I try to generate several times I get success but thats not correct either. I'll keep looking.

avatar image
1

Answer by FabDynamic · Oct 17, 2019 at 07:05 PM

@rob11 Ok I got it working consistently, reliably, and without errors. Basically you get the path and then load from it and then instantiate from that. Its a bit of a funky workaround though so if anyone has a better way lets hear it.

Call PrefabFix.Instantiate instead of prefabUtility.InstantiatePrefab:

 using UnityEngine;
 using UnityEditor;
 
 public class PrefabFix : MonoBehaviour
 {
     public static GameObject Instantiate(GameObject prefab)
     {
         // This works reliably
         string assetpath = PrefabUtility.
             GetPrefabAssetPathOfNearestInstanceRoot(prefab);
         GameObject go =
             AssetDatabase.LoadAssetAtPath(
                 assetpath, typeof(GameObject))
                 as GameObject;
         return PrefabUtility.InstantiatePrefab(go) as GameObject;
 
     }
 }

As you mentioned in your comment it only seems to happen for me when I try to instantiate a prefab from code running in a component of another instance of the same prefab (or something like that). Another thought is I haven't tried caching 'go' and instantiating from that for all future rooms.

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 Bunny83 · Oct 20, 2019 at 02:05 AM 0
Share

This looks as wrong as it can get. $$anonymous$$onoBehaviours are runtime scripts. You can not mix runtimecode and editor code. If this is an editor script it shouldn't be a $$anonymous$$onoBehaviour.


In the OP's question and also in your comment above it's completely unclear where you got your prefab reference from. Note that you can not have a prefab reference to the own prefab in a script on that prefab. That's simply because when a prefab gets instantiated any self references are replaced with references to the instantiated version. That is necessary to keep child and component references within the prefab consistent. However that means if you add a prefab ot a variable on it's own prefab, after instantiation that reference will no longer reference the prefab but the instance.

avatar image
0

Answer by unity_qT8Y9osPinb1jA · Mar 10 at 07:51 PM

I'm having the same issue with Prefabutility.instantiateprefab returning null.

Personally, I just wish this worked more similarly to Instantiate, with the same parameters.

Or maybe Instantiate could accept a true/false parameter that preserves the prefab connection if there's one.

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

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

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

Related Questions

Instantiation Problem 0 Answers

can't save data in prefab and getting an error while press "Apply" in prefab! 0 Answers

can't save data in prefab and getting an error while press "Apply" in prefab! 0 Answers

Buttons won't get interaction in instantiated prefab 0 Answers

Positioning a prefab correctly with Instantiate 3 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