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 snhalsall · Jan 09, 2018 at 12:36 PM · timeif statementactivatebeginners

How to activate/deactivate object in if statements?

Hi, I am trying to make an object start invisible/ deactivated, and then after 4 seconds set it as active. I have the below code, but am very lost on how to make this work. Any help is much appreciated!!

 void Start(){
     gameObject.SetActive (false);
 }

 void Update(){
     if (Time.deltaTime > 4s)
     {
         gameObject.active = true;
     }
     else if (Time.deltaTime < 4s)
     {
         gameObject.active = false;
     }
 }
Comment
Add comment · Show 5
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 PizzaPie · Jan 09, 2018 at 01:20 PM 0
Share

Couple issues here,

  1. Deactivating a GameObject (and therefore all Components attached to it) means that the Update() method will never get called (disabled $$anonymous$$onoBehaviour-> no Update() call). So you can't reactivate it that way.

  2. Time.deltaTime is this (you need to read the documentation before using any Unity related API) probably will never get to 4f, otherwise you need very very slow framerate (

  3. Lastly, it is bad practice to run an if block that always be true after some point, could cause unexpected behaviour and is bad for performance (trivial in this case thought).

So to solve this the direct way would be to use the Invoke method, like this

 void Start()
     {
         gameObject.SetActive(false);
         Invoke("Activate", 2f);                   
     }
 
     void Activate()      
     {
         gameObject.SetActive(true);
     }


Cheers!

avatar image sisse008 PizzaPie · Jan 09, 2018 at 01:49 PM 0
Share

this line will also deactivate all components attached to the gameobject (including this script):

 gameObject.SetActive(false);

avatar image PizzaPie sisse008 · Jan 09, 2018 at 01:59 PM 0
Share

Well that's the point, deactivating the Behaviour doesn't mean that the Start method will stop immediately, it will finish normally and Invoke will be called.

Show more comments
avatar image ExtinctSpecie · Jan 09, 2018 at 02:53 PM 0
Share

try to have a parent object as a controller which will deactivate/activate this object preferably by some trigger event and not in Update() function

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sisse008 · Jan 09, 2018 at 12:38 PM

change gameObject.active = false to gameObject.SetActive(false)

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 sisse008 · Jan 09, 2018 at 01:25 PM 1
Share

I just realized you are trying to deactivate the gameobject holding this script. you cant do that. once the gameobject is deactivated this script (or any other components attached to the game object) will not run anymore.

attach this script to an empty gameobject "controller" and pass the other gameobject by reference;

 public GameObject _object;
  private bool activatedObject = false;

     void Start(){
         _object.SetActive (false);
      }
      void Update(){
          if ((Time.deltaTime > 4s) && !activatedObject)
          {
              _object.SetActive (true);
              activatedObject = true;
          }
        
      }




avatar image ScaniX · Jan 09, 2018 at 02:59 PM 0
Share

Time.deltaTime will also never be larger than 4s. This is the delta time between frames. You need to have a separate float and do something like this:

 private float delayTime;
 
 void Update() {
     delayTime += Time.deltaTime;
     // your other code
 }

avatar image
0

Answer by Anonymous700 · Jan 09, 2018 at 02:04 PM

your time should also be in descending order .

 GameObject.setActive(false);
 GameObject.setActive(true);



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

79 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

Related Questions

Condition not occuring in script 0 Answers

Screensaver when device is idle 1 Answer

How to subtract one time from another? 1 Answer

How often is the update function called ? 1 Answer

converting different time zones 2 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