• 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 MrWalbert · May 21, 2020 at 05:59 PM · objectsinstanceinstantiate prefabprefab-instanceobject reference

Why do my first 50 instances of a prefab have the same properties?

I have been trying to create instances of a prefab in my game and then randomly changing the GameObject's children objects that are created. The prefab is instantiated no issue, when I go to edit the GameObject that was created, all 50 are updated (at least they all come out the same after executing the code.). This only occurs when these 50 game objects are created at the same time. If they are done frame by frame, there is no issue.

     //Generate platforms
     for (int i = meshPlatforms.Length; i < platformsToGenerate; i++)
     {
         //Get the last game object position and figure out the new position.
         GameObject loopPlatform = meshPlatforms[i - 1];
         platformPosition = loopPlatform.transform.position + meshTransform;

         //Create a new platform tile
         Array.Resize(ref meshPlatforms, i + 1);
         GameObject newPlat = Instantiate(tilePrefab, platformPosition, tilePrefab.transform.rotation);
         newPlat.gameObject.tag = slopeChildrenTag;
         newPlat.transform.parent = slopeSet.transform;
         GeneratePlatform(ref newPlat);
         meshPlatforms[i] = newPlat;
     }

     //Update Distance Reader
     GameObject textBox = GameObject.FindGameObjectWithTag(distanceBoxTag);
     TextMesh distanceBox = (TextMesh)textBox.GetComponent("TextMesh");
     distanceBox.text = String.Concat("Distance: ", (Math.Round(Math.Abs(player.transform.position.z))).ToString());
 }
 //This method will do the heavy lifting for running our algorithms to generate wood platforms, sun spots,
 //and regens.
 void GeneratePlatform(ref GameObject platform)
 {
     Transform[] allChildren = platform.GetComponentsInChildren<Transform>(true);
     int isObjectActive = 2;
     System.Random random = new System.Random();
     foreach (Transform platformComponent in allChildren)
     {
         if (platformComponent.tag.Equals(woodPileTag, StringComparison.OrdinalIgnoreCase))
         {
             platformComponent.gameObject.SetActive(true);
             //woodPileAlgorithm
             if (random.Next(1, woodPileOdds) != isObjectActive)
             {
                 platformComponent.gameObject.SetActive(false);
             }

         }
         else if (platformComponent.tag.Equals(spotLightTags, StringComparison.OrdinalIgnoreCase))
         {
             platformComponent.gameObject.SetActive(true);
             //spot light algorithm
             if (random.Next(1, lightSpotOdds) != isObjectActive)
             {
                 platformComponent.gameObject.SetActive(false);
             }

         }
         //else do nothing
     }
 }

I just want the first 50 to be generated the same way the following ones are generated. Not sure if the instance is causing issues or if I am actually changing the prefab. Any tips or places to look are helpful, I have been doing research for about two days now.

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
Best Answer

Answer by MrWalbert · May 21, 2020 at 06:19 PM

Just to follow up, the platforms are all created and spaced correctly, but when I pass the game object into GeneratePlatform, they are all affected the same way.

So I think I found a fix. It was the random number generator. https://stackoverflow.com/questions/8948819/how-do-i-make-my-c-sharp-random-number-generator-change-with-each-time-the-const

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

205 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

Related Questions

Invisible Prefab spawning at location of Player before being instantiated 1 Answer

Can we anyhow store a prefab in a list when we collide into the instance of that prefab casted as gameObject? 1 Answer

Trying to track the BoxColliders of 2 Different Sets of Instantiated GameObjects 1 Answer

Imported object is transparent where it shouldn't be. 0 Answers

Unable to access instantiated GameObject within else statement 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges