• 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
0
Question by Deeblock · May 15, 2018 at 06:10 AM · scripting probleminstantiateprefabpositiondebugging

Instantiate not spawning at correct position

I have a bunch of slots I want to instantiate to form my inventory. Here's my code:

 // Generate inventory and quick slots UI
 quickSlotPositions = new[] { new Vector3(-605, -605, 0), new Vector3(-500,-605,0), new Vector3(-395,-605,0), new Vector3(-290,-605,0), new Vector3(-185,-605,0), new Vector3(185,-605,0), new Vector3(290,-605,0), new Vector3(395,-605,0), new Vector3(500,-605,0), new Vector3(605,-605,0)};
        
 quickSlotContainer = gameObject.transform.Find("Quick Slot Container").gameObject;
  
 for (int i = 0; i < quickSlotPositions.Length; i++)
 {
     print(quickSlotPositions[i]);
     var newQuickSlot = Instantiate(PrefabManager.Instance.prefabDatabase["Quick Slot"], quickSlotPositions[i], Quaternion.identity, quickSlotContainer.transform);
 }

However, all my slots get instantiated at -605, -605, 0 which is the first index of the array. My print statement shows that I'm iterating through the array correctly. What is going wrong? It spawns at my prefab's location, which when set to 0,0,0 spawns it at 0,0,0 instead of my array positions as well.

Comment
Add comment · Show 2
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 TreyH · May 15, 2018 at 12:29 PM 0
Share

That code doesn't compile. Did you copy / paste that directly?

avatar image TreyH · May 15, 2018 at 12:33 PM 0
Share

I set the parent / prefab as inspector assignments as I didn't have your Prefab$$anonymous$$anager and I never use anything with Find in its name. This works fine:

 using UnityEngine;
 
 public class QuickSlot : $$anonymous$$onoBehaviour
 {
     public Transform container;
     public Transform prefab;
 
     // Use this for initialization
     void Awake()
     {
         // Generate inventory and quick slots UI
         Vector3[] quickSlotPositions = new Vector3[] {
             new Vector3(-605, -605, 0),
             new Vector3(-500, -605, 0),
             new Vector3(-395, -605, 0),
             new Vector3(-290, -605, 0),
             new Vector3(-185, -605, 0),
             new Vector3(185, -605, 0),
             new Vector3(290, -605, 0),
             new Vector3(395, -605, 0),
             new Vector3(500, -605, 0),
             new Vector3(605, -605, 0)
         };
 
         for (int i = 0; i < quickSlotPositions.Length; i++)
         {
             print(quickSlotPositions[i]);
             var newQuickSlot = Instantiate(this.prefab, quickSlotPositions[i], Quaternion.identity, this.container);
         }
     }
 }
 


1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · May 15, 2018 at 10:46 AM

Good day.

You can always change the position of the instantiated object after instantiating. As you have stored the instantiated object at newQuickSlot.

 newQuickSlot.transform.localpoation = PositionYouWant

Bye!

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.

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

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

[Solved] Instantiated objects spawn at diferrent x 1 Answer

Instantiate Random Prefab From Folder 1 Answer

Transform position of child is way off. 1 Answer

Cannot convert UnitEngine.Vector3 to UnityEngine.Transform when using .postition 1 Answer

What is different when Instantiate set Prefab Position? 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