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
1
Question by Double_D · Aug 09, 2012 at 01:16 AM · instantiateprefabbuggetcomponentmissing

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:

alt text

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.

bug.jpg (45.0 kB)
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Show 4 · 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 Double_D · Aug 09, 2012 at 01:30 AM 0
Share

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.

avatar image whydoidoit · Aug 09, 2012 at 01:34 AM 0
Share

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?

avatar image Double_D · Aug 09, 2012 at 06:40 PM 0
Share

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.

avatar image Double_D · Aug 09, 2012 at 06:44 PM 0
Share

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?

avatar image
0

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?

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 whydoidoit · Nov 18, 2013 at 10:06 PM 0
Share

Help > Report A Bug

avatar image
0

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.

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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Access a function of an instantiated prefab 1 Answer


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