- Home /
gameobjects and prefabs losing materials
I created 4 cube game objects, and then applied materials to each created from a texture. Then I dragged each game object into a project folder so that I create 4 prefabs. Then my C# code creates instances of the prefabs (when run in play mode). The only problem is that after I turn off play mode, the prefabs and the original game objects no longer have an association to the materials. Is there perhaps something that I'm doing wrong in code that is causing this?
thanks, fb
Hey, FractalBase!
Can you be more specific? So, the materials ARE assigned before you press play, and after you stop playing they do NOT have materials/textures?
Yes, that is correct. Everything is setup before I press play, and after pressing play and then stop playing, the prefabs (and the original game objects) no longer have materials.
Can you show me the class where you are instantiating your prefabs, and where you are assigning any new textures to the instantiations, if anywhere?
Answer by Fractalbase · Oct 05, 2013 at 05:39 PM
private MapGenerationAlgorithmMazeRoom algo;
private GameObject ceiling;
private GameObject floor;
private GameObject floorPassage;
private GameObject wall;
private GameObject cube;
private int xSize = 51;//171;//85;
private int ySize = 51;//73;//35;
// Use this for initialization
private void Start()
{
Randomize.SetSeedTime();
algo = new MapGenerationAlgorithmMazeRoom(xSize, ySize);
algo.DrawMap();
var the3dMap = new MapGrid3D(algo.MapData, 4, 3, 2, 1, 1);
ceiling = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubeCeiling");
floor = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubeFloor");
floorPassage = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubePassage");
wall = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubeWall");
cube = (GameObject) Resources.Load(@"Prefabs/Cube");
var myMap = GameObject.Find("The 3D Map");
Create3DMap(myMap, the3dMap);
var thePlayer = GameObject.Find("The Player");
thePlayer.transform.position = new Vector3(the3dMap.Start.X, the3dMap.Start.Y+3, the3dMap.Start.Z);
}
public void Create3DMap(GameObject myMap, MapGrid3D the3dMap)
{
GameObject created = null;
int pieceIndex = 0;
int max = UInt16.MaxValue;
var floorMaterial = floor.GetComponent<MeshRenderer>().material;
var floorPassageMaterial = floorPassage.GetComponent<MeshRenderer>().material;
var ceilingMaterial = ceiling.GetComponent<MeshRenderer>().material;
var wallMaterial = wall.GetComponent<MeshRenderer>().material;
foreach (var piece in the3dMap.mapPieces)
{
++pieceIndex;
var mapPiece = new GameObject(string.Format("Map Piece: {0}", pieceIndex));
mapPiece.transform.position = new Vector3(piece.Value.Position.X, piece.Value.Position.Y, piece.Value.Position.Z);
mapPiece.transform.parent = myMap.transform;
var floorGameObject = new GameObject(string.Format("The Floor: {0}", pieceIndex));
floorGameObject.transform.parent = mapPiece.transform;
var floorPassageGameObject = new GameObject(string.Format("The Passage Floor: {0}", pieceIndex));
floorPassageGameObject.transform.parent = mapPiece.transform;
var ceilingGameObject = new GameObject(string.Format("The Ceiling: {0}", pieceIndex));
ceilingGameObject.transform.parent = mapPiece.transform;
var wallGameObject = new GameObject(string.Format("The Wall: {0}", pieceIndex));
wallGameObject.transform.parent = mapPiece.transform;
var floorComponent = new List<GameObject>();
var floorPassageComponent = new List<Component>();
var ceilingComponent = new List<Component>();
var wallComponent = new List<Component>();
for (int x = 0; x < piece.Value.Dimensions.X; ++x)
{
for (int y = 0; y < piece.Value.Dimensions.Y; ++y)
{
for (int z = 0; z < piece.Value.Dimensions.Z; ++z)
{
created = null;
MapCell3D cell = the3dMap.GetCell(x, y, z);
var vect = new Vector3((x + piece.Value.Position.X), (y + piece.Value.Position.Y), (z + piece.Value.Position.Z));
/**/
if (cell.Flags == MapCellFlag.Floor)
{
created = (GameObject)Instantiate(floor, vect, Quaternion.identity);
created.transform.parent = mapPiece.transform;
floorComponent.Add(created);
}
else if (cell.Flags == MapCellFlag.Passage)
{
created = (GameObject)Instantiate(floorPassage, vect, Quaternion.identity);
created.transform.parent = mapPiece.transform;
floorPassageComponent.Add(created.GetComponent<MeshFilter>());
}
else if (cell.Flags == MapCellFlag.Wall)
{
created = (GameObject)Instantiate(wall, vect, Quaternion.identity);
created.transform.parent = mapPiece.transform;
wallComponent.Add(created.GetComponent<MeshFilter>());
}
else if (cell.Flags == MapCellFlag.Ceiling)
{
created = (GameObject)Instantiate(ceiling, vect, Quaternion.identity);
created.transform.parent = mapPiece.transform;
ceilingComponent.Add(created.GetComponent<MeshFilter>());
}
}
}
}
CombineInstance[] floorCombine = new CombineInstance[floorComponent.Count];
int count = 0;
foreach (var componentMesh in floorComponent)
{
var floorMesh = componentMesh.GetComponent<MeshFilter>();
floorCombine[count].mesh = floorMesh.sharedMesh;
floorCombine[count].transform = floorMesh.transform.localToWorldMatrix;
floorMesh.gameObject.SetActive(false);
++count;
}
floorGameObject.AddComponent<MeshFilter>();
floorGameObject.GetComponent<MeshFilter>().mesh.CombineMeshes(floorCombine);
floorGameObject.AddComponent<MeshCollider>();
floorGameObject.AddComponent<MeshRenderer>();
floorGameObject.GetComponent<MeshRenderer>().material = floorMaterial;
floorGameObject.SetActive(true);
foreach (var componentMesh in floorComponent)
{
componentMesh.transform.parent = null;
}
floorComponent.Clear();
floorComponent = null;
CombineInstance[] floorPassageCombine = new CombineInstance[floorPassageComponent.Count];
count = 0;
foreach (var componentMesh in floorPassageComponent)
{
var floorPassageMesh = (MeshFilter) componentMesh;
floorPassageCombine[count].mesh = floorPassageMesh.sharedMesh;
floorPassageCombine[count].transform = floorPassageMesh.transform.localToWorldMatrix;
floorPassageMesh.gameObject.SetActive(false);
++count;
}
floorPassageGameObject.AddComponent<MeshFilter>();
floorPassageGameObject.GetComponent<MeshFilter>().mesh.CombineMeshes(floorPassageCombine);
floorPassageGameObject.AddComponent<MeshCollider>();
floorPassageGameObject.AddComponent<MeshRenderer>();
floorPassageGameObject.GetComponent<MeshRenderer>().material = floorPassageMaterial;
floorPassageGameObject.SetActive(true);
foreach (var componentMesh in floorPassageComponent)
{
componentMesh.gameObject.transform.parent = null;
}
floorPassageComponent.Clear();
floorPassageComponent = null;
CombineInstance[] ceilingCombine = new CombineInstance[ceilingComponent.Count];
count = 0;
foreach (var componentMesh in ceilingComponent)
{
var ceilingMesh = (MeshFilter) componentMesh;
ceilingCombine[count].mesh = ceilingMesh.sharedMesh;
ceilingCombine[count].transform = ceilingMesh.transform.localToWorldMatrix;
ceilingMesh.gameObject.SetActive(false);
++count;
}
ceilingGameObject.AddComponent<MeshFilter>();
ceilingGameObject.GetComponent<MeshFilter>().mesh.CombineMeshes(ceilingCombine);
ceilingGameObject.AddComponent<MeshCollider>();
ceilingGameObject.AddComponent<MeshRenderer>();
ceilingGameObject.GetComponent<MeshRenderer>().material = ceilingMaterial;
ceilingGameObject.SetActive(true);
foreach (var componentMesh in ceilingComponent)
{
componentMesh.gameObject.transform.parent = null;
}
ceilingComponent.Clear();
ceilingComponent = null;
CombineInstance[] wallCombine = new CombineInstance[wallComponent.Count];
count = 0;
foreach (var componentMesh in wallComponent)
{
var wallMesh = (MeshFilter) componentMesh;
wallCombine[count].mesh = wallMesh.sharedMesh;
wallCombine[count].transform = wallMesh.transform.localToWorldMatrix;
wallMesh.gameObject.SetActive(false);
++count;
}
wallGameObject.AddComponent<MeshFilter>();
wallGameObject.GetComponent<MeshFilter>().mesh.CombineMeshes(wallCombine);
wallGameObject.AddComponent<MeshCollider>();
wallGameObject.AddComponent<MeshRenderer>();
wallGameObject.GetComponent<MeshRenderer>().material = wallMaterial;
foreach (var componentMesh in wallComponent)
{
componentMesh.gameObject.transform.parent = null;
}
wallComponent.Clear();
wallComponent = null;
}
}
// Update is called once per frame
private void Update()
{
}
Answer by Catlard · Oct 07, 2013 at 06:00 AM
Ok, so I think the reason this is happening is because you're loading from resources, which causes you to pass the prefabs by reference, instead of by value. I would create a "safe" gameobject and try changing that instead of the thing you actually load from resources.
okay, I changed my code to user public variables, and I've assigned the prefabs to the variables in the inspector. However, when I play the scene, I get the following error:
UnassignedReferenceException: The variable floor of '$$anonymous$$ap3D' has not been assigned.
(Note: I'm using UnityVS w/ Visual Studio 2012.)
Here's the changed code:
public class $$anonymous$$ap3D : $$anonymous$$onoBehaviour
{
private $$anonymous$$apGenerationAlgorithm$$anonymous$$azeRoom algo;
public GameObject ceiling;
public GameObject floor;
public GameObject floorPassage;
public GameObject wall;
//private GameObject cube;
private int xSize = 51;//171;//85;
private int ySize = 51;//73;//35;
// Use this for initialization
private void Start()
{
Randomize.SetSeedTime();
algo = new $$anonymous$$apGenerationAlgorithm$$anonymous$$azeRoom(xSize, ySize);
algo.Draw$$anonymous$$ap();
var the3d$$anonymous$$ap = new $$anonymous$$apGrid3D(algo.$$anonymous$$apData, 4, 3, 2, 1, 1);
//ceiling = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubeCeiling");
//floor = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubeFloor");
//floorPassage = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubePassage");
//wall = (GameObject)Resources.Load(@"Prefabs/DungeonElements/CubeWall");
//cube = (GameObject) Resources.Load(@"Prefabs/Cube");
var my$$anonymous$$ap = GameObject.Find("The 3D $$anonymous$$ap");
Create3D$$anonymous$$ap(my$$anonymous$$ap, the3d$$anonymous$$ap);
var thePlayer = GameObject.Find("The Player");
thePlayer.transform.position = new Vector3(the3d$$anonymous$$ap.Start.X, the3d$$anonymous$$ap.Start.Y+3, the3d$$anonymous$$ap.Start.Z);
}
Your answer
Follow this Question
Related Questions
New prefab system problem 1 Answer
Models in prefab are broken after updating .fbx file 0 Answers
Prefabs losing Material connections after re-opening scenes. Bug? 3 Answers
Change prefab Material 0 Answers