Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Oceris · Nov 26, 2017 at 08:28 PM · prefabprefabsprefab-instance

Making multiple instances of a prefab,Instantiating multiple instances of a prefab

Hey, im making a 2d infinite runner and im having an issue with making multiple instances of one prefab. I have a script that is attached to the prefab and is run when the prefab is added to the scene, it changes positions of elements in prefab. When i make the first instance of the prefab it works fine, it randomizes positions of elements, but next instances of prefab do not randomize position of elements, they are copies of first instance instead of new instances based on prefab located in assetss. Im struggling to fix this problem, any help would be appreciated,Hello, im making 2d procedural generated infinite runner. I generate my platforms base on a prefab which has a script attached that relocates platforms inside this prefab when prefab is instanted. I create them in chunks 3 at a time, the problem is that the first one instance of prefab is having its platforms relocated but the second and the third have the same platforms locations, as if the third was copy of the second from scene, not a new Instance of prefab from assets. Im new to unity and struggling to resolve this issue. Help would be really appreciated.

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 rvilgalys · Nov 26, 2017 at 08:36 PM

Hard to say without knowing the code. Where are you generating the random information?

First guess would be that you're starting with the same Random.seed for each instance so they're picking the same "random" numbers.

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 Oceris · Nov 26, 2017 at 08:54 PM 0
Share

@rvilgalys Ill explain my flow, i make instances of prefabs in a loop, every fuction call i make 3 instances. First one is ok, but the second and the third have the same position of elements inside. Below is the part of the script which relocates elements.

         private void randomizePosition()
         {
             System.Random random = new System.Random();
             foreach(Transform child in children.GetRange(0,2))
             {
                 Vector3 newPos = new Vector3(random.Next((-tileWidth / 2), (-tileWidth / 4)), random.Next((-tileHeight/ 2), 0));
                 Vector3 newSCale = new Vector3(random.Next(1, 3),child.localScale.y, 0);
                 child.transform.localPosition = newPos;
                 child.transform.localScale = newSCale;
             }
             foreach (Transform child in children.GetRange(2,2))
             {
                 Vector3 newPos = new Vector3(random.Next((0), (tileWidth / 2)), random.Next((-tileHeight / 2), 0));
                 Vector3 newSCale = new Vector3(random.Next(1, 3), child.localScale.y, 0);
                 child.transform.localPosition = newPos;
                 child.transform.localScale = newSCale;
             }
         }

It is weird because the function that makes 3 instances of prefab is called in update() function and every time new prefabs instances are made the first one is ok but the second and third are the same And yea, ive checked and the "random numbers" are the same but its impossible for this to be just a coincidence. It happens like 100 times in a row

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

90 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

Related Questions

Some components missing in the prefab (clone) 0 Answers

Instantiated Prefabs Are Destroyed After 4 instantiations 1 Answer

prefab navagent doesnt work,prefab navmesh doesnt work 1 Answer

how to replace multiple game objects with different transform component with same prefab? 1 Answer

Can't modify BoxCollider center/size on base prefab 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