• 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 Dec 10, 2018 at 04:14 AM by ragefordragons for the following reason:

Other

avatar image
Question by ragefordragons · Dec 05, 2018 at 03:42 AM · instantiateprefabsfor-loop

Prefabs wont instantiate using a for loop?

So I have a for loop that triggers when an enemy dies and rolls a dice to see if it drops certain items. The for loop works fine, but 'lootBag' will not instantiate in the scene. There is no errors, and the console can return both the object itself and its value as well as position and none of it is null. What was also strange is when it said that it had spawned but wasn't in the scene, I checked its transform position, and it was exactly where another version of the prefab existed in the scene, so I thought maybe it was somehow getting refrence from that prefab in the scene or something. But, I deleted the prefab from the scene and the for loop returns its old postion. This probably has nothing to do with it, but I thought it might be relevant. I am very confused, and was wondering if anyone has any ideas?

Code:

 public Item[] drops;
 public float[] chance;

 private float dice;

 private GameObject lootBag;



 public void dropLoot()
 {

     for (int i = 0; i < drops.Length; i++)
     {
         if (drops[i] != null) //checks if the item in the array is actually there.
         {

             if(chance[i] != 0) //checks if the drop chance is > 0.
             {
                 dice = Random.Range(0, 100); //rolls dice for drop.

                 if (dice <= chance[i]) //checks if it landed in the chance range.
                 {
                     lootBag = Resources.Load<GameObject>("drops/DroppedItem");

                     Instantiate(lootBag, transform);
                 }

             } else
             {
                 Debug.Log(drops[i].name + "'s drop chance is zero. Why did you do that?"); //prints if the items chance is zero.
             }              
         } 
     }
     

 }

}

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by bburtson09 · Dec 05, 2018 at 05:32 AM

"If you have multiple Resources folders you cannot duplicate use of an asset name."

Resources.Load , Loads an asset stored at path in a folder called Resources. The path is relative to any folder named Resources inside the Assets folder of your project.

Unity docs for Resources.Load

I'm going with the wild assumption that you may have more than one resources folder in assets and/or you probably created a couple test-runs of your prefab.

Just have a hunch you that you are loading a prefab you didn't expect.

hope i nailed it good luck

Comment

People who like this

0 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 ragefordragons · Dec 05, 2018 at 03:07 PM 0
Share

I looked and I dont have any other Resources folders... Also instantiating other prefabs hasn't had this issue so far, its just this one. What do you mean by 'test runs of my prefab'? do you mean a copy with the same name in the project folder or in the scene?

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

121 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

Related Questions

Clicker game instantiate prefab depending on gold per click 0 Answers

How can you make a Instanciate prefab follow a scene object? 1 Answer

Instantiating Prefabs from a MonoBehaviour-derived class using an array -> compiling but not rendering [SOLVED] 0 Answers

Creating text at the position of the gameobjects in world 0 Answers

Replace instantiated objects in an array 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