- Home /
Setting the parent of a transform which resides in a prefab error
I'm getting the following error:
"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."
ON the following bit of code:
 public void GenerateSectors() {
         
         ClearLocations();
         GameObject locationPrefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
         locationPrefab.AddComponent("Location");
         
         for (int i = 0; i < numberOfLocations; i++) {
             Vector3 rand = Random.insideUnitSphere * 90f;
             rand.y = 0.0f;
             
             
             GameObject newLocation = (GameObject)Instantiate(locationPrefab, rand, Quaternion.identity);
             
             newLocation.transform.parent = mapParent.transform;
             locations.Add(newLocation.transform);
             
         }
         
         Destroy(locationPrefab);
     }
I have searched the internet and the forums for an answer as to why this is happening. AND I have found several people who encountered a similar problem. I believe I Have made the needed changes to my code so it would work (according to information found through searching.) but alas, I have been unsuccessful.
I'm trying to create a bunch of cubes, set their parent to a predefined object, and add them to a list so I can manipulate and destroy them later.
I know what usually generates this error, and I don't see it in this script. I hacked your code a bit to get it to compile and run, and I had no problems (compile time or run time):
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Bug25c : $$anonymous$$onoBehaviour {
     
 void Start() {
     GenerateSectors ();    
     }
     
 List<Transform> locations = new List<Transform>();
     
  public void GenerateSectors() {
  
        //ClearLocations();
        GameObject locationPrefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
        locationPrefab.AddComponent("Rigidbody");
  
        for (int i = 0; i < 100; i++) {
          Vector3 rand = Random.insideUnitSphere * 5.0f;
          // rand.y = 0.0f;
  
          GameObject newLocation = (GameObject)Instantiate(locationPrefab, rand, Quaternion.identity);
  
          newLocation.transform.parent = transform;
          locations.Add(newLocation.transform);
  
        }
  
        Destroy(locationPrefab);
     }
 
 }
Once in a rare while I have a issue where Unity does not recompile a script that I have modified. I suggest closing Unity, make a modification to the script and save it, and reopen Unity.
Thanks for the attempt. I tried restarting as you suggested. But still getting the error.
Answer by Amineeee · Jun 12, 2017 at 08:45 AM
facing the same problem , am pretty damn sure there is nothing wrong with the code , for instance i am instantiating two objects in a scene with the same way , one is working fine and assigned to the right parent the other is not and getting this error
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                