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
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613024525im_/https://answers.unity.com/themes/thub/images/avi.jpg)