Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by looak · Jun 06, 2011 at 10:02 AM · gameobjectchildren

When loading scene, reset GameObject references..

I'm generating my mesh with collision from code and I have separated my mesh to render and my collision mesh into two different game objects with the rendering GameObject being the parent to the Collider GameObject.

When I generate my visual mesh I also generate my collision mesh so that they correspond to each other.

Basicly I do:

 private GameOjbect coll = null;
 
 GenerateMesh()
 {
     //Do funky mesh generation stuff.
     GenerateCollision();
 }
 
 GenerateCollision()
 {
     if( coll == null )
     {
         coll = new GameObject();
         coll.transform.parent = gameObject.transform;
     }
     else
     {
     //clean mesh etc...
     }
     //Do funky mesh generation stuff for collision
 }

So my problem is when I reaload the scene, swap scenes, restart unity or what ever the "private GameObjet coll = null" is set and it regenerates the collision so it gets duplicated. And when I change the mesh settings( from editor script and recalculate the mesh etc ) the old collision stays the way it was since the connection is lost beteween them.

How do I in the Start or Awake function set the collision GameObject reference in code... if there is one, because If its non existing I want it to be null.

Is this clear enough or am I just rabbling words that no one understands?

Comment
Add comment · Show 4
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 looak · Jun 06, 2011 at 11:27 AM 0
Share

So now I'm using this GameOjbect.Find("name"); stuff but since I'm generating my stuff from code everything gets the same name, and thereby I can't guarantee that they will get the corresponding collision mesh. And that two different visual meshes get the same coll.

So I tried GameObject.Find("/name"); because the script reference stated "If name contains a '/' character it will traverse the hierarchy". But it doesn't work either, so Im guessing that it is going from the "root" GameObject ins$$anonymous$$d of the one I'm doing the call from?

Anywho... how do I access my child GameObjects? This shouldn't be hard in my opinion since the whole structure of Unity is based on hierarchies of GameObjects... But a simple routine as to getting the first child of a GameObject isn't existing? Isn't that weird?

avatar image Mike 3 · Jun 06, 2011 at 11:39 AM 0
Share

You can use transform.Find to find children, or you can enumerate over transform to get at the children: foreach(Transform child in transform) { do stuff with child }

avatar image looak · Jun 06, 2011 at 11:42 AM 0
Share

Ye, thanks.. Figured it out with transform.Find("name").gameObject.... feels kinda weird though.. but it works.

avatar image almo · Jun 06, 2011 at 01:52 PM 0
Share

Code reformatted for clarity

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Jun 06, 2011 at 12:10 PM

You posted further questions in your comment, so I'll try to answer them all best I can.

Let's start with your comment first.

How do I access my child GameObjects?

To access child objects, you access them through the transform. It's actually the transform that provide the hierarchal setup, not the game objects themselves, contrary to what some people intuitively have thought in the past. You may try to find the children by name or iterate through the children one by one using the undocumented transform.GetChild(index), so you can do more fine grained searching operations if needed be. You may also enumerate through all of them with a foreach loop since transform expose an enumerator (also undocumented).

Then there was the original question;

How do I in the Start or Awake function set the collision GameObject reference in code... if there is one, because If its non existing I want it to be null.

I am not entirely sure about what you are trying to achieve. Do you mean you have a child object you want a reference to when the scene is reloaded? Is it even there still? Anyhow, given you have a child you want to keep the reference to, just do something like:

 GameObject coll;
 
 void Awake()
 {
     FetchColliderChild();
 }
 
 void FetchColliderChild()
 {
     var colliderName = "Collider Object"; // Change to the name of your child
     var colliderTransform = transform.Find(colliderName);
 
     if (colliderTransform)
     {
         coll = colliderTransform.gameObject;
         Debug.Log("Found collider.");
     }
     else
     {
         coll = null;    
         Debug.LogError("Could not find collider: " + colliderName, this);
         Debug.Break();
     }
 }

And when I change the mesh settings( from editor script and recalculate the mesh etc ) the old collision stays the way it was since the connection is lost beteween them.

I am not entirely sure I understand what you mean.

You're modifying/creating a mesh through an editor script, correct? Shouldn't you also create the collider mesh in that same go? What is the reason for postponing the collider generation until runtime?

Another option would be to make your reference serialized or public, but I find it quite hard understanding exactly your problem...

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 looak · Jun 06, 2011 at 02:10 PM 0
Share

I am creating the collider at the same time as I'm generating the mesh. Not generating any meshes in runtime since that would most likely kill the performance?

I've actually solved it, and it makes sense that it's the transforms that provide the hierarchy rather then the GameObjects them selves.

The problem was that when I had created my meshes and saved the scene to later reopen it and continue working the saved variable coll was set to null again and therefor the script remade the collision mesh even though there already was one in its hierarchy.

I came to solve it by using the transform.Find("name").gameObject; since that seemed to just search its own hierarchy and would return null if there were no GameObjects of that name in its hierarchy and thereby solving my problem.

I might have been a bit unclear in my description of my problem due to frustration and unfamiliarity with unity as well as writing in english. I'm sorry for that.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to move a gameObject without affecting its childrens' positions? 2 Answers

How to Access Components in Children of GameObject? 1 Answer

Does a large number of components will affect performance ? 2 Answers

Using parent tag on whole gameObject 1 Answer

C# Children of GameObject has it's script disabled after SetActive(false) and SetActive(true) 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