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 bloodpatch42 · Oct 20, 2020 at 09:48 PM · instantiate prefabinstantiationordergame object

Unity Instantiates GameObjects in a wrong order

Trying my best to make this short. I have been working with Unity for about 2 days now and I'm an amateur. I have to make an Extended Tower of Hanoi game. For that I want to first ask the user for the number of Disks they want the puzzle to be solved with, then according to that number diskCount I want to instantiate the objects (prefabs) in certain positions relative to diskCount. I'm instantiating the objects using for loops but it ends up making the last object of the loop the first one and it happens on other loops of instantiation too. Am I missing a point here or is it like a bug with Unity?

alt text Here's the code:

 public class Spawn : MonoBehaviour
 {
     public static int diskCount = 6;
     public GameObject[] disk = new GameObject[diskCount];
     public Disk[] Disk = new Disk[diskCount];
     public GameObject[] peg = new GameObject[3];
     public Peg[] Peg = new Peg[3];
     public GameObject Base;
     void Start()
     {
         float n = (float)(3 + (diskCount / 3) - 1);
         BaseSpawn(n);
         for (int i = 0; i < 3; i++)
             SpawnPegs(i, n);
         for (int i = 0; i < diskCount; i++)
             SpawnDisks(i, n);
     }
 
     void BaseSpawn(float n)
     {
         Instantiate(Base, new Vector3(0, 0, 0), Quaternion.identity);
         Base.transform.localScale = new Vector3(n - 1, 0.5f, n * 3);
         Base.name = "Base";
 
     }
 
     void SpawnPegs(int i, float n)
     {
         Peg[i] = new Peg(new Vector3(0, n / 3.0f, (i - 1) * (Base.transform.localScale.z / 2.7272727273f)), new Vector3(0.3f, (n / 3.0f) - 0.25f, 0.3f), diskCount / 3);
         Instantiate(peg[i], Peg[i].position, Quaternion.identity);
         peg[i].transform.localScale = Peg[i].scale;
         peg[i].name = $"Peg {i + 1}";
     }
 
     void SpawnDisks(int i, float n)
     {
         int currentPeg = i % 3;
         //disks positionings data on pegs here
         Disk[i] = new Disk(new Vector3(Peg[currentPeg].position.x, Peg[currentPeg].level, Peg[currentPeg].position.z), new Vector3(40f + (i * 5f), 40f + (i * 5f), 4), currentPeg);
         Peg[currentPeg].level += 0.08f;
         //disks GameObject size and appearance and spawning here
         Instantiate(disk[i], Disk[i].position, Quaternion.Euler(-90f, 0, 0));
         disk[i].transform.localScale = Disk[i].scale;
         disk[i].name = $"Disk {i + 1}";
     }
 }

sorry for the mess I know it's not the best code you've seen

screenshot-585.png (172.1 kB)
Comment
Add comment · Show 3
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 Klarzahs · Oct 21, 2020 at 12:36 PM 0
Share

Can't find the error, but what happens if you create them one after another, e.g. by waiting for a "space" press each time? Are they still listed in the wrong order in the hierarchy?

avatar image bloodpatch42 Klarzahs · Oct 21, 2020 at 01:52 PM 0
Share

I tried putting a timer before calling the spawn methods in each iteration, still each time the first Object should be the last object.

avatar image rh_galaxy · Oct 21, 2020 at 03:35 PM 0
Share

What does the disk[] array look like in the Inspector?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Oct 21, 2020 at 07:15 PM

Like rh_galaxy said you essentially used Instantiate wrong. Instantiate takes a source reference of the object that should be cloned and returns the cloned object. It doesn't do anything to the source object. You manipulate the source prefab after you instantiate your object so the next time you instantiate an object you will clone your modified prefab and then modify the prefab again.


That means since you modify the actual prefab in your project, those changes will persist. So when you re-run your game the first element you instantiate will have the scale you set to the last object. As a result all your objects will essentially be rotated one place to the left.


When you instantiate an object you should use the reference that is returned by the Instantiate method and don't touch the actual prefab you used as source. Something like this:

 var clone = Instantiate(disk[i], Disk[i].position, Quaternion.Euler(-90f, 0, 0));
 clone.transform.localScale = Disk[i].scale;
 clone.name = $"Disk {i + 1}";
Comment
Add comment · Show 2 · 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 21, 2020 at 07:28 PM 1
Share

Funny I made a very simple towers of hanoi some time ago. You can actually play it here, though it was just a proof of concept and very straight forward implementation in 215 lines of code ^^.

avatar image bloodpatch42 · Oct 21, 2020 at 09:25 PM 0
Share

Yes yes thank you very much for your reply. I realised my mistake and I took the approach you suggested and it is working beautifully now! Thank you so much again!

avatar image
2

Answer by rh_galaxy · Oct 21, 2020 at 04:06 PM

You set the disk[] array prior to calling Instantiate in the Inspector? The same for peg[]. That means you have to drag existing GameObjects to each array (also making it impossible to have diskCount set by the user). Seams you don't use Instantiate as effective as it can be if I'm not lost here. That is if they are all the same model, only scale makes them different?

I would have one Peg object (with a Peg script attached) and one Disk object (with a Disk script attached) as a start (as publics set in the Inspector). Then instantiate THAT object for each disk (and peg). Then set scale and name after like you do now, and set other properties like color or anything else by calling the Disk script for the new object (return value of Instantiate()).

Seams you have too many copies of too many objects now.


To answer your question about wrong order, it is because the object you give name to is the original object that you make a copy of. But you set the name after you have already made a copy of it. If it is like I suspect that you have dragged one Disk object 6 times to the array, that means you will rename it and the next time you make a copy it will be called that ("Disk 2(Clone)" instead of "Disk 3" like you intended).

Comment
Add comment · Show 2 · 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 bloodpatch42 · Oct 21, 2020 at 09:24 PM 1
Share

Thank you so much for the answer. Yes I corrected the first part of your answer and declared only one Disk and Peg as GameObject and instantiated them in a for loop. And about the diskCount being declared by me yes I am aware of that and I am tetsting it as an example. I understood the second part as well. Than you very much!

avatar image rh_galaxy · Oct 22, 2020 at 04:46 AM 1
Share

You can get rid of some variables and avoid having two arrays each for keeping track of the disks and pegs. At least this is how I have done in my projects.

 public class Spawn : $$anonymous$$onoBehaviour
 {
     public Disk diskObjOriginal; //drag your disk prefab here in the Inspector
     private Disk[] disks = new Disk[diskCount]; //the disk clones
     ...
 
     void SpawnDisks(int i, float n)
     {    
         Disk diskNew = Instantiate(diskObjOriginal);
         diskNew.Init(i, position, scale, color, anything_you_want);
         disks[i] = diskNew;
     }
 }
 
 public class Disk : $$anonymous$$onoBehaviour
 {
     public void Init(int diskNr, Vector3 position, Vector3 scale, )
     {
         //properties
         name = "Disk" + diskNr.ToString();
         transform.position = position;
         transform.localScale = scale;
 
         //example for setting the Disk material
         $$anonymous$$aterial material = Resources.Load("Disk$$anonymous$$at1", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
         GetComponent<$$anonymous$$eshRenderer>().material = material;
 
         gameObject.SetActive(true);
     }
 }

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

139 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

Related Questions

How can I instantiate a unique game object? 2 Answers

Instantiated Bullets Not Picking Up Correct Position/Rotation 2 Answers

How to instantiate an object at mouse position? 1 Answer

Instantiating enemies in spawn points not working on Android Build 0 Answers

GameObject Parent-Child instantiation order 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