Jump to content

Is gameobject.SetActive broken?


Recommended Posts

Gleetings fellow codng type persons. I'm trying to track down a bug in Kerbal Konstructs, specifically that sometimes when entering the flight scene or space center scene, third party statics fail to appear.

Here's a nice chunk of code to consider:

		public void updateCache(Vector3 playerPos)
{
foreach (StaticObject obj in childObjects)
{
float dist = Vector3.Distance(obj.gameObject.transform.position, playerPos);
Debug.Log("KK: Distance is " + dist);
bool visible = (dist < (float) obj.getSetting("VisibilityRange"));
if (visible != obj.gameObject.activeSelf)
{
Debug.Log("KK: Setting " + obj.gameObject.name + " to visible =" + visible);
// obj.gameObject.renderer.renderer.enabled = true;
obj.gameObject.SetActive(visible);
Debug.Log("KK: .layer is " + obj.gameObject.layer);
Debug.Log("KK: .activeInHierarchy is " + obj.gameObject.activeInHierarchy);

// ASH 06112014
// What if SetActive isn't actually properly activating children?
Transform[] gameObjectList = obj.gameObject.GetComponentsInChildren<Transform>();
List<GameObject> rendererList = (from t in gameObjectList where t.gameObject.renderer != null select t.gameObject).ToList();
foreach (GameObject renderer in rendererList)
{
Debug.Log("KK: Child renderer.visible is " + renderer.renderer.isVisible);
Debug.Log("KK: Child renderer.enabled is " + renderer.renderer.enabled);
}
}
}
}

The bug is difficult to reproduce but it involves hanging around a launch location and playing with the camera for a bit. Then reverting or recovering. This takes you to the space centre where the third party statics fail to appear. When the bug occurs, this suddenly decides that each gameobject doesn't have any children anymore:

Transform[] gameObjectList = obj.gameObject.GetComponentsInChildren<Transform>();

It's almost as if all the poor little children have suddenly been disowned. It's a very inconsistent bug and I'm out of ideas what to do next.

Edited by AlphaAsh
Removed some redundant code that could confuse things
Link to comment
Share on other sites

When the bug occurs, this suddenly decides that each gameobject doesn't have any children anymore:

Transform[] gameObjectList = obj.gameObject.GetComponentsInChildren<Transform>();

GameObject.SetActive will not enable children if they've been disabled (activeSelf == false). The GetComponentsInChildren method you used won't return inactive components. That's probably why you think they vanished. Use the overload version Edited by xEvilReeperx
Link to comment
Share on other sites

That might be a band-aid. Assuming StaticObject obj is a parent GO of a hierarchy of same-static related components like meshes, I'd be wondering how the sub-GOs got disabled in the first place. You need only disable the parent to disable all children

Link to comment
Share on other sites

That's what's worrying me too. KK does only explicitly de-activate a parent and no-where does it purposefully de-activate children. More to the point, 99% of the time that code up there works fine. Actually reproducing the circumstances where it suddenly thinks all the children are inactive is proving problematic and therefore makes a band-aid the only thing I can think of atm.

Link to comment
Share on other sites

That torpedoes my theory, then. It's possible Squad is iterating over the GameDatabase model list and deactivating stuff themselves. Unlikely, but you could easily test it by detaching your static object model (specifically, the GO you get from GameDatabase.GetModel[in]) from its parent and possibly replacing its entry in GameDatabase.databaseModel with some fake GameObject (don't forget to DontDestroyOnLoad the fake one)

Link to comment
Share on other sites

That torpedoes my theory, then. It's possible Squad is iterating over the GameDatabase model list and deactivating stuff themselves. Unlikely, but you could easily test it by detaching your static object model (specifically, the GO you get from GameDatabase.GetModel[in]) from its parent and possibly replacing its entry in GameDatabase.databaseModel with some fake GameObject (don't forget to DontDestroyOnLoad the fake one)

That's a good idea. Thanks chap.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...