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 hari ram · Apr 28, 2014 at 01:01 PM · gameobjectactivatedeactivate

How to activate a gameobject via scripting ???

In my game I need to deactivate a gameobject when I click it occurs and make it activated in all other time.I use the following code.

 public GameObject me;
 
     void Update ()
     {
         me.active = true;
 
     }
 
     void OnMouseDown()
     {
         me.active = false;
     }


I am attaching this script to the gameobject which must be deactivated.But I am able to deactivate when I click it but it is not at all re-activating. Is there anything I have missed out?? I have also used gameobject.active,gameobject.SetActive().Nothing worked.

Comment
Add comment · Show 2
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 robertbu · Apr 28, 2014 at 03:14 PM 0
Share

I'm confused about what you are trying to do here. First I assume that 'me' is assigned the same game object this script is attached to? Note 'if' this worked, then your object would only be deactivated for the remainder of the frame. On the next frame it would again be active. Is that really want you want to attempt to do? If that's really want you want, then @hexer's approach will work:

 void Update ()
 {
    renderer.enabled = true;
    collider.enabled = true; // Assu$$anonymous$$g there is a collider
 }
 
 void On$$anonymous$$ouseDown()
 {
     renderer.enabled = false;
     collider.enabled = false; // Assu$$anonymous$$g there is a collider
 }   
avatar image Andres-Fernandez · Apr 28, 2014 at 03:21 PM 0
Share

If you enable renderer and collider components in the update function again, object will self-enable those components the following frame.

3 Replies

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

Answer by KillMobil · Apr 28, 2014 at 01:13 PM

after the object has been deactivated the Update () function does not get called any more.

the handling of the reactivation will need to be handled be another object that preferable handles most of the proccess.

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
avatar image
1

Answer by Assieman · Apr 28, 2014 at 01:41 PM

You are disabling the object, so also the game component, the script itself, so it cannot execute anything anymore.

Comment
Add comment · Show 1 · 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 hari ram · May 15, 2014 at 05:07 AM 0
Share

Ya @assieman you'r right!! But I achieved my goal by disabling rendering & box collider and re-enabling it when I needed.

avatar image
1

Answer by Hexer · Apr 28, 2014 at 02:25 PM

Instead of de-activate the object, you want to de-activate just something like the collider and/or the rendering. Try something like this.

 void OnMouseDown()
 { 
 GameObject.GetComponent(SpriteRenderer).enabled=false;
 }

This lets you disable the rendering of the object.(Instead of disabling the whole object) This code might not work because I mainly use UnityScript but hopefully this can help you out abit

  • Doesn't Void Update() repeat the command each second? it means that, when deactivating. The object will immediatly be enabled again.

I guess this can be solved by using if statements.

Comment
Add comment · Show 1 · 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 Andres-Fernandez · Apr 28, 2014 at 03:24 PM 0
Share

I would be:

 void On$$anonymous$$ouseDown() {
         bool isEnabled = gameObject.GetComponent<Renderer>().enabled;
         gameObject.GetComponent<Renderer>().enabled = !isEnabled;
     }

If you set the collider component to false, then the object will never get the On$$anonymous$$ouseDown event again (unless being enabled from another source).

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to activate/deactivate a component(Animator) when a specific game object was activated 0 Answers

Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers

ReActivate object with null movement ?? 2 Answers

Activating/Deactivating GameObject Problem :( 3 Answers

Cannot deactivate / activate gameobject, even in editor! 6 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