Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Sorade · Oct 12, 2017 at 06:52 PM · instantiateprefab

Instantiating Prefab fails but no error message

Hi all,

I have an issue instantiating prefabs. I generate tiles for my game and I want to display an effect (the prefab) on top of each tile. I tried everything I could think off. The prefab is recognized as a gameobject (I checked using Debug.Log) I can also execute code before and after without any problem.

The prefab is in a singleton and that seems to be causing some problems. Anybody came across this before ? This is my singleton which contains an array of GameObject with my prefabs : infoSkins

 public class TileManager : MonoBehaviour {
         public GameObject[] infoSkins;
     #region
     //Awake is always called before any Start functions
     void Awake()
     {
         //Check if instance already exists
         if (instance == null)
             //if not, set instance to this
             instance = this;
         //If instance already exists and it's not this:
         else if (instance != this)
             //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
             Destroy(gameObject);
     }
     #endregion //Singleton setup
 

I then call this method when the game starts:

     public void Initialise(Vector2 currentPos, int row, int col){
         pos = currentPos;
             InstantiateInfoVFX();
     }

     public void InstantiateInfoVFX()
     {
         if (targetCategory.type == "Fire")
         {
             GameObject.Instantiate(TileManager.instance.infoSkins[0], new Vector3(pos.x, 0.3f, pos.y), Quaternion.identity); //This is not working, no objects is added to the hierachy
             Debug.Log("TEST"); //this however prints and the game runs fine
         }
      }


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

Answer by ghostmode · Oct 13, 2017 at 07:04 AM

Can you check if the prefab you're instantiating from is null. Also, you may as well check the instance while you're at it. I know you said you already checked something similar, but this really helps a lot to narrow things down.

 var prefab = TileManager.instance.infoSkins[0];
 if (prefab == null) {
     Debug.Log("the prefab is NULL!");
 } else {
     Debug.Log("the prefab is NOT null");
 }
 
 var instance = Instantiate(prefab, new Vector3(pos.x, 0.3f, pos.y), Quaternion.identity);
 if (instance == null) {
     Debug.Log("the instance is NULL!");
 } else {
     Debug.Log("the instance is NOT null");
 }

 

Edit: Another thought, you might have 2 instances of your singleton, one of which is being quietly destroyed. Replace your tile manager's Awake with the following:

 private void Awake() {
     if (instance == null) {
         instance = this;
     } else {
         Debug.LogError("Found multiple TileManagers! Destroying instance.");
         Destroy(gameObject);
     }
 }

This will show an error when you accidentally have multiple tile managers in your scene.

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 Sorade · Oct 17, 2017 at 12:54 PM 0
Share

thank you for the reply. I checked that the prefab is not null. It does return a GameObject. I will try out the multiple Singleton fix tonight though !

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

102 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

Related Questions

Instantiate PreFab with C# Script 5 Answers

Instantiate A Prefab 1 Answer

Instantiate in front of player (and give velocity) 1 Answer

Change prefab before instantiation 1 Answer

How do I re-instantiate original prefab after a game object inside has been destroyed? 2 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