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 /
  • Help Room /
avatar image
0
Question by afloemans · Feb 03, 2021 at 07:51 PM · scripting problemprefab

How to modify many prefab objects in a script?,

New to Unity, in my project I generate a large number (1000) of identical objects ("friends") sequentially. These "friends" are clones of a prefab. The "friends" fall by gravity onto a "terrain" surrounded by a wall "Cube1", They collect randomly on the "terrain". After this I want to freeze them in place (remove their ability to move as individuals, or to fuse them together so they move as one object, or remove their RigidBody component or IN SOME WAY prevent them from falling through the "terrain" after the next step ). Next after collecting the friends on the terrain I remove the "terrain" MeshCollider so that additional falling objects pass through the "terrain". This script generates the "Setting parent of a transform which resides in prefab Asset is disabled to prevent data corruption" This ERROR, stops continued execution of the script. Suggestions.

 public GameObject Cube1;
 public GameObject friend;
 public GameObject contaminant;
 public int numberOfFriends;//number of friends to generate 
 public int numberOfContams;//total number of contaminants to generate
 public GameObject terrain;


 void Start()  // Start is called before the first frame update
 {
     //Time.timeScale = 1.0f;//set speed of frames 1 = 60 frames per second        
     StartCoroutine(DropFriends());      
 }
 

 IEnumerator DropFriends() //secquential drop  friend events
 {//
  
     InvokeRepeating("GenerateObjectsF", 2.0f, .5f); //generate sperical deep bed filter

     yield return new WaitForSeconds(15);

     Debug.Log("1st checking");

     friend.transform.SetParent(Cube1.transform);//setfriends to be child of cube1
     //make collection of friends stationary objects before contams are dropped

     Debug.Log("2nd checking");

     yield return new WaitForSeconds(3);

  
     terrain.GetComponent<MeshCollider>(). enabled = false;//remove Meshcolllider to allow contams fall thru

     InvokeRepeating("GenerateObjectsC", 2.0f, 0.5f); //generate spherical contminsnts
     yield return new WaitForSeconds(3);

     Debug.Log("3rd checking");
 }
 

 void GenerateObjectsF() //names objects to be generated
 {
     GenerateObject(friend, numberOfFriends); // calls GenerateObject to instantaite friends
     //friend is a prefab added to the inspector level gen script, and numberOfFriends is set in the inspector
     if (--numberOfFriends == 0) CancelInvoke("GenerateObjectsF");
 }

 void GenerateObjectsC() //names objects to be generated
 {
     GenerateObject(contaminant, numberOfContams); // calls GenerateObject to instantaite friends
     //friend is a prefab added to the inspector level gen script, and numberOfFriends is set in the inspector
     if (--numberOfContams == 0) CancelInvoke("GenerateObjectsC");
 }

 void GenerateObject(GameObject go, int amount) //defines the generation of objects "go"
 {
     if (go == null) return;
     for(int i=0; i< 1; i++)
     {
         //changing y in vector3 here sets height where objs are generated
         GameObject tmp = Instantiate(go);       //defines temporary obj puts game object "go" in the scene 
         Vector3 randomPoint = GetRandomPoint(); //generates a vector named "randompoint" by calling GetRandomPoint()
         tmp.gameObject.transform.position = new Vector3(randomPoint.x, 30.0f, randomPoint.z);
         //defines where to place tmp object that put it instantaited  at position given by the rand point vector coordinates
     }
     //Time.timeScale = 0.0f; //stops running by setting time between frames to 0
 }

 Vector3 GetRandomPoint() //generate a random vector3
 {
     int xRandom = 0;
     int zRandom = 0;
     xRandom = (int)Random.Range(-10, 10);//generate random number for x
     zRandom = (int)Random.Range(-10, 10);//generate random number for z
     return new Vector3(xRandom, 0.0f, zRandom);//form new vector3 and return it
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

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

Variables in a script on a prefab do not store runtime changes 0 Answers

How do I reference the player within an instantiated prefab? 0 Answers

Mesh missing in prefab 0 Answers

How to Create variants with different scripts interact with player 1 Answer

Showing a high score in the menu 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