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 Banditmayonnaise · Sep 24, 2019 at 06:40 PM · inspectorvideocountdownappear

Make Button appear after a sudden amount of time

I'm making a Interactive Film. Everything is working, i just want my buttons to appear after a sudden amount of time. I made script, that i have attached to my buttons, but when I play the game the button won't appear again. I just get this error: Coroutine couldn't be started because the the game object 'Choice_1' is inactive! UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) Button_Fade_In:Start() (at Assets/Scripts/Button_Fade_In.cs:19)

I wan't the time factor to be adjustable in the inspector.

My Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 
 public class Button_Fade_In : MonoBehaviour
 {
     [SerializeField]
     private float _sec;
     public GameObject _objectToActivate;
    
 
     private void Start()
     {
         if (gameObject.activeInHierarchy)
             gameObject.SetActive(false);
 
         StartCoroutine(ActivationRoutine());
     }
 
     private IEnumerator ActivationRoutine()
     {
 
         yield return new WaitForSeconds(_sec * Time.deltaTime);
 
         gameObject.SetActive(true);
     }


,I'm making a Interactive film for a school project. I already made 2 buttons that can start a new video clip, but the problem is the button is visible and clickable from the start of every video. I want the button to appear after a sudden amount of time. And I also want the time to be adjustable in the inspector.

I'm a noob at C# using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;

 public class Button_Fade_In : MonoBehaviour
 {
     [SerializeField]
     private float _sec;
     public GameObject _objectToActivate;
    
 
     private void Start()
     {
         if (gameObject.activeInHierarchy)
             gameObject.SetActive(false);
 
         StartCoroutine(ActivationRoutine());
     }
 
     private IEnumerator ActivationRoutine()
     {
 
         yield return new WaitForSeconds(_sec * Time.deltaTime);
 
         gameObject.SetActive(true);
     }

I made this script but it doesn't work... Whenever i click Play, my buttons won't appear again. I got this error: Coroutine couldn't be started because the the game object 'Choice_1' is inactive! UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) Button_Fade_In:Start() (at Assets/Scripts/Button_Fade_In.cs:19)

Comment
Add comment · Show 1
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 WarmedxMints · Sep 24, 2019 at 06:51 PM 0
Share

Run the coroutine on a different gameobject. I suggest calling it Ui$$anonymous$$anager.

Also, remove the * Time.deltaTime from the WaitForSeconds parameter, it is not required.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by talyh · Sep 24, 2019 at 06:59 PM

I think you meant to have _objectToActivate.SetActive(true) (same in your Start. The current code disables the gameObject, so that coroutine won't run.

(Side note: not sure if this is attached to the button itself. if it is, you can keep the gameObject active but not visible, setting the button alpha to 0 until it's time to show, or using a CanvasGroup to hide multiple 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
avatar image
0

Answer by Tarball · Sep 24, 2019 at 10:43 PM

What I would do is make a parent object with child button objects in its hierarchy. Attach your script to the parent object (which is always active) and turn your button objects on and off, while your script continues running. Also, one more thing. Consider using something besides the unreliable WaitForSeconds. Try this instead:

 bool myBool;
 yield return new WaitUntil(() => myBool);

That will wait until myBool == true before executing subsequent code.

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
0

Answer by surfuay · Sep 25, 2019 at 01:30 AM

you've got a lot of good answers.

personally i would just make a coroutine and call it in the start method. in the coroutine just state how long you want to wait.

ie

  void start ()
  {
        startcoroutine(buttonappear());
  }

  ienumerator buttonappear()
  {
       yield return new waitforseconds(90);
       button.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

120 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 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

Make GUI box appear and disappear 2 Answers

Where should the video file be located when using Handheld.playfullscreenmovie 1 Answer

make object to appear from bottom up 2 Answers

Export an animation from unity at a fixed frame rate. 1 Answer

Objects Appearing 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