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 Lypheus · Apr 14, 2010 at 03:15 AM · visible

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.

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

2 Replies

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

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.

Comment
Add comment · Show 9 · 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 Lypheus · Apr 14, 2010 at 03:16 PM 0
Share

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!

avatar image Lypheus · Apr 14, 2010 at 03:23 PM 0
Share

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.

avatar image Cyclops · Apr 14, 2010 at 03:28 PM 0
Share

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

avatar image Lypheus · Apr 14, 2010 at 03:51 PM 0
Share

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.

avatar image Cyclops · Apr 14, 2010 at 04:22 PM 0
Share

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? :)

Show more comments
avatar image
0

Answer by Eric5h5 · Apr 14, 2010 at 05:10 AM

Maybe you mean transform.Find?

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

No one has followed this question yet.

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


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