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 /
avatar image
0
Question by Lukey695 · Jul 17, 2021 at 12:53 AM · instantiatelayoutparenting

Problems setting parent of instantiated objects

Can anybody tell me where i have gone wrong, I am trying to instantiate prefabs to a grid layout group component and for any given variable of slots i try to create i get a extra 50% of that given number that are unparented clones of clones,, i.e if i want to create 10 slots it will add 10 slots to the inventory grid but it will spawn 5 unparented clones of clones ,20 will spawn 10 unparented unwanted clones

public class SlotControl : MonoBehaviour { public GameObject inventorySlotPrefab; public Transform inventoryGrid;

 public int MaxSlots;
 [SerializeField]private int currentNumberOfSlots;

 private void Start()
 {
     //CountSlots();
     //if (currentNumberOfSlots != MaxSlots)
     //{
     //    SlotCreation();
     //}
 }
 private void Update()
 {
     //if (MaxSlots != currentNumberOfSlots)
     //{
     //    SlotCreation();
     //}
     if (Input.GetKeyDown(KeyCode.Return))
     {
         if (MaxSlots != currentNumberOfSlots)
         {
             Debug.Log("Forced Update");
             SlotCreation();
         }
     }
 }
 public void CountSlots()
 {
     {
         GameObject[] _inventorySlotPrefab = GameObject.FindGameObjectsWithTag("inventorySlotPrefab");
         Debug.Log("Found Slot Tags");
         currentNumberOfSlots = _inventorySlotPrefab.Length;
         Debug.Log("Slots Counted");
     }
 }
 public void SlotCreation()
 {
     CountSlots();
     for (int i = 0; i < MaxSlots; i++)
     {
         //var obj = inventorySlotPrefab.transform;
         var obj = Instantiate(inventorySlotPrefab, Vector3.zero, Quaternion.identity, transform);
         obj.transform.SetParent(inventoryGrid.transform);
         if (currentNumberOfSlots < MaxSlots)
         {
             Debug.Log("Not Enough Slots");
             Instantiate(obj);
             CountSlots();
         }
         else if(currentNumberOfSlots > MaxSlots)
         {
             Debug.Log("Too Many Slots");
             Destroy(obj);
             CountSlots();
         }
         else if(currentNumberOfSlots == MaxSlots)
         {
             Debug.Log("Slot Count OK");
         }
         
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_BUJQdslYm7EuFQ · Jul 17, 2021 at 08:27 AM

Problem is that you are creating new slots, while all slots has not appeared yet. You should check number of spawned slots after the for cycle end it`s work.

   public void SlotCreation()
     {
         List<GameObject> spawnedSlots = new List<GameObject>(); //All slots spawned in for cycle. 
         //If line above causes an error like "could not find type or namespace "List <>"", add "using System.Collections.Generic;" at the top of skript.
 
         for (int i = 0; i < MaxSlots; i++)
         {
             //var obj = inventorySlotPrefab.transform;
             spawnedSlots.Add(SpawnSlot());
             spawnedSlots[spawnedSlots.Count - 1].transform.SetParent(inventoryGrid.transform);
         }
 
         CountSlots(); //counting slots after spawning
 
         if (currentNumberOfSlots < MaxSlots)
         {
             int Difference = MaxSlots - currentNumberOfSlots;
             Debug.Log("Not Enough Slots");
             for (int i = 0; i < Difference; i++) //spawnes missing quantity of slots
             {
                 spawnedSlots.Add(SpawnSlot());
             }
         }
         else if (currentNumberOfSlots > MaxSlots)
         {
             int Difference = currentNumberOfSlots - MaxSlots;
             Debug.Log("Too Many Slots");
             for (int i = 0; i < Difference; i++) //destroys excess amount of slots
             {
                 int LastSpawnedSlotID = spawnedSlots.Count - 1; //ID of last spawned slot
 
                 Destroy(spawnedSlots[LastSpawnedSlotID].gameObject);
                 spawnedSlots.RemoveAt(LastSpawnedSlotID);
             }
         }
         else if (currentNumberOfSlots == MaxSlots)
         {
             Debug.Log("Slot Count OK");
         }
 
         GameObject SpawnSlot() //I took this out to prevent copying the code
         {
             return Instantiate(inventorySlotPrefab, Vector3.zero, Quaternion.identity, transform);
         }
     }
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
avatar image
0

Answer by Lukey695 · Jul 17, 2021 at 09:44 PM

@unity_BUJQdslYm7EuFQ thank you so much i'm kind of new to this i spent ages trying to figure the problem myself, there were a few ways i tried that almost worked but lesson learned i suppose.

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 Lukey695 · Jul 18, 2021 at 12:40 AM 0
Share

Ok ran into a few issues after swapping MaxSlot And Current Slot i fixed an out of range argument else if (currentNumberOfSlots > MaxSlots) int Difference = MaxSlots - currentNumberOfSlots ; //these 2 but the other issue I have been having is that when the MaxSlots variable changes during runtime to a number lower than the CurrentNumberOfSlots the script spawns a number of slots equal to the max slots value repeatedly on call and doesnt destroy the excess slots prefabs

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

145 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

Related Questions

How do I update player tilt in multiplayer? 0 Answers

How to Parent a Cloned Object to Another Cloned Object 1 Answer

Layout Group not working when instantiate from prefab 1 Answer

Error : Transform.parent not working properly. 1 Answer

Creating text at the position of the gameobjects in world 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