- Home /
Accessing prefab data before instantiation breaks prefab?
Sorry, total nOOb-BuckeT here, but this seems like a bug? But I could just be doing something wrong. All I know is that aliens are stealing my mesh out of my prefab!
So here is a simple script to illustrate my point.
//**********Begin Code*******************
#pragma strict
var fabNew : Rigidbody;
function Start () {
var fabDimensions : Mesh = fabNew.GetComponent(MeshFilter).mesh;
Debug.Log("Max X = " + fabDimensions.bounds.max.x);
Debug.Log("Min X = " + fabDimensions.bounds.min.x);
Debug.Log("Center = " + fabDimensions.bounds.center);
Debug.Log("Size X = " + fabDimensions.bounds.size.x);
Debug.Log("Size Y = " + fabDimensions.bounds.size.y);
Debug.Log("Extents = " + fabDimensions.bounds.extents);
}
function Update () {
}
//**********End Code*******************
I was trying to get the dimensions of the prefab without needing to instantiate it in the scene. I want to be able to input any prefab into the script and just store the dimensions/bounds in a Vector3 for later use. The weird thing is that you get the correct debug output the first time you hit "Play", but then it breaks your prefab. This is the sequence of said breaking:
I would think that wanting to do this or something similar with an un-instantiated prefab would be useful, if not common? Even if you're not supposed to be able to do this, it at least should't break your prefab?
I tried sharedMesh with similar results.
The MeshFilter that's being abducted by aliens is on the highest parent/child level of the prefab "Generic Platform" as seen in the associated image above.
No errors are thrown on the first execution, I'm running build 3.5.4.f1 (e412d194b23b), and no errors are thrown if I change the script to put a Debug.Log around the GetComponent function.
Any help is greatly appreciated!
Thanks!
EDIT: Forgot to say that if I instantiate the prefab into the scene first, everything works fine.
Answer by whydoidoit · Aug 09, 2012 at 01:22 AM
It looks like a bug - but it's never really a good idea to go around believing that your prefab is a real object when its not instantiated in the scene. An ExecuteInEditMode on a script and a capturing of the bounds during OnEnable or OnDisable would probably work and then the values would be stored in the prefab (presuming that they are serialized).
Thanks for the prompt reply, but (Like I stated) I'm a noob. It's going to take me some time to figure out how to do what it is you're saying exactly.
If you put @script ExecuteInEdit$$anonymous$$ode then you code will run while the game is being edited. This allows you to do things like capture that data you are after.
OnDisable is called when the object is still enabled so it should be ok to get the data then and store it in a public variable or a private one marked with @SerializeField. That might work if you are setting the mesh in the editor. Awake is called when the object is started or dropped into the scene - but I guess the question is - do you really need this information to be stored rather than calculated?
So I'm at work and can't really dive into figuring out how to implement what you said, but maybe explaining what I'm trying to accomplish with this script would shed some light on the matter.
The purpose of the script is to take any rigidbody prefab from an exposed variable, and then instantiate a user defined quantity of this prefab into the scene. These subsequent instances would need to be spaced with specific distances between their bounds.
I figured that the solutions were the following: 1) Query the prefab for the needed bounds before looping to instantiate the prefabs. (What I'm currently trying to do) 2)Go right into the loop to instantiate the prefabs, and query the bounds on every iteration. 3)Just like #2, except have an if statement to evaluate if the Vector3 var for its bounds is null/empty/(0,0,0) and subsequently set it only on the first iteration of the loop.
The first example seems like the most efficient, because it doesn't have those extra steps in the loop.
Also, your comment about this see$$anonymous$$g like a bug leads me to believe that you agree that my code should have worked? That you don't agree with my implementation, but regardless it should have worked?
Answer by Agostino · Nov 18, 2013 at 10:01 PM
This is also happening to me. Using Unity 4.2.2.
Really, is there a way to report this bug?
Answer by azakh · May 28, 2014 at 12:10 PM
It is better to use MeshFilter.sharedMesh with prefabs to avoid unnecessary implicit mesh duplication.
MeshFilter.mesh instantiates new mesh and looks like it's not connected to the prefab object and is destroyed after exiting play mode.
Bug is reported.
Your answer
Follow this Question
Related Questions
Instantiate Prefabs. Errors. 2 Answers
Get unity to recognize prefab in C# 2 Answers
Variable of prefab has not been assigned 2 Answers
Play iTween on instantiated prefab 1 Answer