- Home /
Hiding Objects Not Working (Active/Enable)
The following script is attached to the root node of my soldier object in the asset hierarchy:
function Start( ) { toggleMarker( false ); }
function OnMouseDown( ) { if( GameState.activeUnit == this ) { toggleMarker( false ); GameState.activeUnit.animation.Play( "staying" ); GameState.activeUnit = null; GameState.travelTo = null; } else { var clip = Resources.LoadAssetAtPath("Assets\\Sounds\\roger.wav", typeof( AudioClip ) ); AudioSource.PlayClipAtPoint(clip, transform.position); GameState.activeUnit = this; toggleMarker( true ); }
}
function toggleMarker( isOn ) { // gameObject.Find( "SelectUnitMarker" ).active = isOn; Find( "SelectUnitMarker" ).active = isOn; }
I've tried off the transform as well, none of these appear to work - what is the problem here, it seems a pretty trivial operation. All I want to do is get an object, attached to my soldier (an arrow to indicate if they are active or not) and toggle its visibility.
Answer by Cyclops · Apr 14, 2010 at 02:41 PM
In the commented code, you have gameObject.Find() in lower-case, and it should be upper-case to indicate you are calling a Class function. Also, it's good practice to store a GameObject reference, rather than repeatedly calling Find(). Try this code:
private var uMarker : GameObject;
function Start() { uMarker = GameObject.Find("SelectUnitMarker"); if (uMarker == null) Debug.Log("uh-oh"); }
function ToggleMarker(isOn) { uMarker.active = isOn; // Method 1. uMarker.transform.renderer.enabled = isOn; // Method 2. }
If you use Method 1, and the GameObject has children that you also want deactivated, you want to use SetActiveRecursively().
Another way to just make it invisible, as opposed to entirely deactivating it, is Method 2 listed above, turning off the Renderer.
And Eric5h5 is right, if the GameObject is a direct child of the soldier object, you can use transform.Find(). If it's further down the chain, say a grandchild, you have to give it the full pathname, though - see the link for example.
Update - I slightly mis-interpreted the question, I saw the gameObject reference, and missed the part about the object being attached. So to add:
GameObject.Find() - searches the entire scene. Returns a GameObject.
transform.Find() - searches for direct children, or grandchildren if you give a full path name. Returns a Transform.
Sounds good, I'll take a look tonight when I get home from work.
As to the storing of references, this is not on the critical path so its questionable whether the cost of holding onto those references over a ga$$anonymous$$g session vs a O(1) ($$anonymous$$ap?). I'll profile this later in the iteration.
Now, GameObject vs gameObject? I interpret that as a class level call - so is unity3d assu$$anonymous$$g the starting search path to be the current object on a GameObject.Find call - I was thinking gameObject.Find() to start searching contextually .. bleh, ok I'll look and learn tonight - THAN$$anonymous$$S!
I guess to clarify for above, if I have: A::SelectUnit$$anonymous$$arker and B::SelectUnit$$anonymous$$arker And I'm in B::Start() - does a GameObject.Find() search for the first "SelectUnit$$anonymous$$arker" and return A::SelectUnit$$anonymous$$arker? I'm wanting to narrow it to searching off of B so that B::SelectUnit$$anonymous$$arker is returned by ensuring I'm only traversing the tree attached to B and not A.
Yes, if you look at the docs for GameObject: http://unity3d.com/support/documentation/ScriptReference/GameObject.html, you'll see that Find is listed in the Class functions, not the regular object functions. But as @Eric5h5 noted, using the transform's Find() will search for direct children (or grandchildren if you use a path name).
Ok, I saw this - now the problem I had was when I did a transform.Find() the result came back as a transform and didn't have the active member variable. It seems like you've answered that too though with $$anonymous$$ethod 2 ... I'll just have to try it out at home - thanks again for your help, will test @ home and post back.
Heh - the relationship between Transform and GameObject is one of the more confusing ones :) But once you get the Transform, you can reference its GameObject, like this: sometransform.gameObject.active = false; Note the lowercase gameObject. Does that clear things up, or confuse them up? :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612162753im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make always visible(rendering) weapons in FPS? 2 Answers
Making a Game object appear and dissapear 3 Answers
i need script c# scrollview object hide/unhide 0 Answers
EditorWindow is visible 2 Answers
3D Text visible from/in x distance/place 0 Answers