- Home /
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?
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?
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 }
Ye, thanks.. Figured it out with transform.Find("name").gameObject.... feels kinda weird though.. but it works.
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...
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
Follow this Question
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