- Home /
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. after unity upgrade.
Hello, i upgraded from unity 5.0 to the latest version and i now get an error it feels very much like an unity bug and i need a way to avoid it:
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:SetParent(Transform) background:create(String, GameObject, Int32, Single, Single, Single, Single, Single, Single) (at Assets/scripts/background.cs:42) background:Start() (at Assets/scripts/background.cs:63)
the code file looks like this:
using UnityEngine;
using System.Collections.Generic;
using System;
using System.IO;
public class background : MonoBehaviour {
System.Random randomi;
GameObject mapprefab;
public bool enabled=true;
string path;
string npath;
string spath;
GameObject spaceb;
GameObject slayer;
GameObject nlayer;
RectTransform rspaceb;
Dictionary<string,GameObject> gameobj= new Dictionary<string, GameObject>();
static public GameObject getobjbn(string name) {
UnityEngine.Object[] listing = (UnityEngine.Resources.FindObjectsOfTypeAll(typeof(GameObject)));
foreach (GameObject l in listing) {
if (name == l.name) {
return l;
}
}
Debug.LogError ("object " + name + " notfound");
GameObject ihatec;
ihatec = new GameObject ();
return ihatec;
}
void create(string folder, GameObject parent, int count, float minscale, float maxscale, float minscalez, float maxscalez, float divisor, float rotationspeed) {
var res= Resources.LoadAll<GameObject> (folder);
int counter=1;
while (counter++ < count) {
GameObject prefab= GameObject.Instantiate( res[randomi.Next(res.Length-1)]);
prefab.layer = 10;
try {
prefab.GetComponent<rotate> ().enablepos = true;
}
catch { }
gameobj.Add (folder+counter,prefab);
prefab.transform.SetParent (parent.transform); // <<<<<<<<<<<<<<<<<<- this is the line of the error!
prefab.transform.localScale = new Vector3 ((float) randomi.NextDouble (minscale, maxscale), (float) randomi.NextDouble (minscale, maxscale), (float) randomi.NextDouble (minscalez, maxscalez));
prefab.transform.localPosition = new Vector3 ((float)randomi.NextDouble (-1* rspaceb.rect.width/divisor,rspaceb.rect.width/ divisor), (float) randomi.NextDouble (-1* rspaceb.rect.width / divisor, rspaceb.rect.width/ divisor), (float) randomi.NextDouble (0,1000));
//MeshRenderer a= prefab.GetComponent<MeshRenderer> ();
//a.additionalVertexStreams.colors[0].a =0;
//prefab.GetComponent<MeshRenderer> ().additionalVertexStreams.colors.a = 0.5f;
}
}
// Use this for initialization
void Start() {
if (!enabled) return;
randomi = new System.Random ();
mapprefab = getobjbn ("mapprefab");
path = Path.Combine ("maps", "spacebackground");
npath = Path.Combine (path, "nebula");
spath = Path.Combine (path, "star");
spaceb = getobjbn ("spacebackground");
slayer = getobjbn ("starlayer");
nlayer = getobjbn ("nebulalayer");
rspaceb = spaceb.transform.GetComponent<RectTransform> ();
create (npath, nlayer, 4,32500,60440,1,2,5f,1);
Your answer
Follow this Question
Related Questions
Game Object is Instantiating on same position 2 Answers
Multiple Cars not working 1 Answer
Destroying assets is not permitted to avoid data loss. 0 Answers
How do you trigger a specific collider using OnTriggerEnter? 2 Answers
Spawned tree not falling 1 Answer