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
1
Question by Fellewis · Jan 07, 2019 at 12:27 AM · lightif-statementskeyactiveframe

My object desactive and active multiple time in a frame

Hello i have a very simple problem in my script, the problem is when i press my key, the fonction desactive and active multiple time, i dont understand because i use getkeydown who normally use one frame. alt text alt text

if someone can explain the problem it be very very nice and thankful, sorry for bad english

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LightController : MonoBehaviour
 {
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown("f"))
         {
             if (gameObject.activeSelf == true)
             {
                 gameObject.SetActive(false);
                 Debug.Log("Desactive light");
 
             }
             if (gameObject.activeSelf == false)
             {
                 gameObject.SetActive(true);
                 Debug.Log("Active light");
             }
         }
     }
 
 }


afters.png (73.4 kB)
important.png (53.2 kB)
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
2
Best Answer

Answer by Bunny83 · Jan 07, 2019 at 12:38 AM

Look careful at the line where i added the comment:

 void Update()
 {
     if (Input.GetKeyDown("f"))
     {
         if (gameObject.activeSelf == true)
         {
             gameObject.SetActive(false);
             Debug.Log("Desactive light");
         }
         
         // You have no else here.
         
         if (gameObject.activeSelf == false)
         {
             gameObject.SetActive(true);
             Debug.Log("Active light");
         }
     }
 }

Since you have no else between your two if statements, they are both executed one after another when you press your button. So if the object is active when you press the button your first if statement is true and you deactivate the object. Now the execution continues to the second if statement. Since you just deactivated the object the second if statement is also true and you activate the object again within the same frame. Your code only run once.


You should do something like this:

 void Update()
 {
     if (Input.GetKeyDown("f"))
     {
         if (gameObject.activeSelf)
         {
             gameObject.SetActive(false);
             Debug.Log("Desactive light");
         }
         else
         {
             gameObject.SetActive(true);
             Debug.Log("Active light");
         }
     }
 }

Or even simpler:

 void Update()
 {
     if (Input.GetKeyDown("f"))
     {
         gameObject.SetActive(!gameObject.activeSelf);
     }
 }

This will simple toggle the state since we pass the opposite of the current state as the new state (note the !).


edit

As it turns out the approach of deactivating the gameobject doesn't work since the gameobject can not activate itself again once it's deactivated. To toggle (enable / disable) just the Light component you can do:

 private Light m_Light;
 
 void Awake()
 {
     m_Light = GetComponent<Light>();
 }
 
 void Update()
 {
     if (Input.GetKeyDown("f"))
     {
         m_Light.enabled = !m_Light.enabled;
     }
 }

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 Magso · Jan 07, 2019 at 12:35 AM

It's checking both ifs at once, so it's turning off and on in a single frame. Use "else if" on the last if statement, that way it will only do the first statement that returns true and skip the rest.

Comment
Add comment · Show 7 · 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 Fellewis · Jan 07, 2019 at 12:44 AM 0
Share

Thanks now i understand the problem but I use this script but now i cant active light anymore

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LightController : $$anonymous$$onoBehaviour
 {
 
     // Update is called once per frame
     void Update()
     {
         if (Input.Get$$anonymous$$eyDown("f"))
         {
             if (gameObject.activeSelf == true)
             {
                 gameObject.SetActive(false);
                 Debug.Log("Desactive light");
 
             }
             else if (gameObject.activeSelf == false)
             {
                 gameObject.SetActive(true);
                 Debug.Log("Active light");
             }
         }
     }
 }

avatar image Bunny83 Fellewis · Jan 07, 2019 at 12:50 AM 0
Share

Well, of course you can't ^^ When you deactivate the whole gameobject the update callback won't be called anymore. The gameobject basically shot itself in the foot.


You have two possible solutions:

  • First, don't deactivate the gameobject but maybe only disable a certain component. You probably want to disable / enable the Light component of the gameobject.

  • The other option is to put the light controller on a seperate gameobject that doesn't get deactivated and add a reference to the actual light gameobject and activate / deactivate this referenced object and not the object the script is attached to.

avatar image Magso Bunny83 · Jan 07, 2019 at 01:29 AM 0
Share

I completely missed that the object was being set inactive. Your first solution is easier.

 gameObject.GetComponent<Light>().enabled = true/false;

However just seen GetComponent light is now obsolete!?

Show more comments

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

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

Related Questions

Want to make appear and disappear a Light and a Particle Emiter 1 Answer

Trying to make Collider Activate with key code 1 Answer

Button to create object??? 1 Answer

Save gameobject active 1 Answer

Any way to check if an object is active? 1 Answer


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