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 /
avatar image
2
Question by Fractalbase · Oct 03, 2013 at 06:24 AM · prefabsmaterials

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

Comment
Add comment · Show 3
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
avatar image Catlard · Oct 03, 2013 at 07:17 AM 0
Share

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?

avatar image Fractalbase · Oct 04, 2013 at 02:59 AM 0
Share

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.

avatar image Catlard · Oct 04, 2013 at 05:51 AM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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()
         {
 
         }
 
Comment
Add comment · Share
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
avatar image
0

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.

Comment
Add comment · Show 1 · Share
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
avatar image Fractalbase · Oct 13, 2013 at 11:06 PM 0
Share

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

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

15 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

Related Questions

When creating a 2D array as a grid of cubes they are all the same colour,My array of gameObjects are all the same colour, even when I set them differently 2 Answers

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


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