Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jamesster · Sep 27, 2010 at 08:41 PM · javascriptdisableenable

Find GameObject and activate/deactivate it

I have 3 characters in the scene - C1, C2, and C3. When the player presses the number 1 on the keyboard, I want C2 and C3 to be disabled, while C1 is set to be enabled. And when the player presses 2, I want C1 and C3 to be disabled while C2 is set to be enabled. All 3 characters are generated and created accordingly by scripting when the scene is loaded (They are custom characters defined by the user). Right now, this is what the script looks like:

var Character1 : GameObject; var Character2 : GameObject; var Character3 : GameObject;

function Update() { if(Input.GetKeyDown(KeyCode.Alpha1)) { Character1 = GameObject.Find("C1"); //Code for enabling/disabling characters goes here }

if(Input.GetKeyDown(KeyCode.Alpha2)) { Character2 = GameObject.Find("C2"); //Code for enabling/disabling characters goes here }

if(Input.GetKeyDown(KeyCode.Alpha3)) { Character3 = GameObject.Find("C3"); //Code for enabling/disabling characters goes here } }

The problem is, I don't know how to tell it to disable and enable the right ones. The script is attached to an empty GameObject called Character Controller. Any ideas on how I might tell it to disable, for instance, C2 and C3 but enable C1 if 1 is pressed? All the characters have multiple children, so they must be disabled as well. I am fairly new to JavaScript and am trying to learn how this works. Thank you!

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
1

Answer by Jesse Anders · Sep 28, 2010 at 01:12 AM

I'm not quite sure I understand the question - it looks like you know how to acquire references to each of the three characters, so what part are you stuck on? It looks like you're pretty much there.

In your example code, there's no reason for the 'Character*' variables not to be local (at least not that I can see). However, it can sometimes be advantageous to cache references to game objects rather than acquiring them on demand. (Sometimes though the latter is necessary, or at least preferable, and I doubt you'll see any difference in performance either way in this case.)

As for your question, it seems the code to activate one character and disable the other two would look something like this (untested):

GameObject.Find("C1").SetActiveRecursively(true);
GameObject.Find("C2").SetActiveRecursively(false);
GameObject.Find("C3").SetActiveRecursively(false);

Is that what you're looking for?

Comment
Add comment · Show 2 · 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 jamesster · Sep 28, 2010 at 09:56 PM 0
Share

$$anonymous$$ind of, however, there is a slight problem. So let's say I start off with C1 enabled, but C2 and C3 disabled. I hit 2 to switch to character 2, and C1 is disabled. However, because C2 is disabled, the script can't seem to find C2 to enable it. The result? No character is enabled, and once all 3 are disabled, the game can't find a single one, making the game more or less frozen with no way to do anything. Got any ideas?

avatar image justinl · Oct 05, 2012 at 05:24 PM 0
Share

You could store the references to these characters in a manager class that way they'll always be available.

avatar image
1

Answer by dingben · Dec 05, 2010 at 12:58 PM

See these 2 posts:

[ http://answers.unity3d.com/questions/29756/how-to-deactivate-a-parent-and-its-children/30267#30267 ]

[ http://answers.unity3d.com/questions/30274/is-there-any-command-that-can-reference-deactivated-objects ]

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

1 Person is following this question.

avatar image

Related Questions

How to disable/enable SSAOEffect or other specific Components? 1 Answer

How to Disable/Enable another Script with Triggers? 1 Answer

Disable C# Script from Java Script 1 Answer

Getting an objects Children 1 Answer

Temporarily removing rigidbody and configurable joint and then replacing them 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