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 AttilaZold · Dec 06, 2015 at 09:59 PM · scripting problemonenable

Gameobject's Script component doesn't start IEnumerator, nor InvokeRepeating when activated... Why?

I have a gameObject with a script on it that enables and disables (one at a time) SpriteRenderers using IEnumerator. Imagine dialog images showing up on a character. These Sprites (the dialog images) are children of gameObject. After all the Sprites finish the gameObject is set to destroy itself (also done with courutine).

This works perfectly if gameObject is enabled on game start. MY ISSUE is when a second gameObject with a different Script that handles different Sprites (second conversation with player) gets enabled after some player's conditions have been met (player collects something): this is when the IEnumerator doesn't work; only the first Sprite is enables (function of void OnEnabled). Here is the showText script for first gameObject (this courutine is counting very good): using UnityEngine; using System.Collections;

namespace UnityStandardAssets.ImageEffects { public class ShowText1 : MonoBehaviour {

     public SpriteRenderer Dialog1;
     public SpriteRenderer Dialog2;
     public SpriteRenderer Dialog3;
     public SpriteRenderer Dialog4;

     public GameObject PLAYER;
     public Rigidbody2D pl_Rigidbody2D;
     public VignetteAndChromaticAberration vignette;

     public RigidbodyConstraints2D constraints;

     void Start()
     {
         Dialog2.enabled = false;
         Dialog3.enabled = false;
         Dialog4.enabled = false;

         pl_Rigidbody2D = PLAYER.GetComponent<Rigidbody2D>();
         vignette = Camera.main.GetComponent<VignetteAndChromaticAberration>();
         vignette.enabled = true;

         Destroy(gameObject, 19);


         StartCoroutine(MyCoroutine());
     }

     IEnumerator MyCoroutine()
     {
         //This is a coroutine
         Dialog1.enabled = true;
         pl_Rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;

         yield return new WaitForSeconds(3);    //Wait one frame

         Dialog1.enabled = false;
         Dialog2.enabled = true;

         yield return new WaitForSeconds(3);

         Dialog2.enabled = false;
         Dialog3.enabled = true;

         yield return new WaitForSeconds(6);

         Dialog3.enabled = false;
         Dialog4.enabled = true;

         yield return new WaitForSeconds(6);

         Dialog4.enabled = false;
         pl_Rigidbody2D.constraints = RigidbodyConstraints2D.None;
         vignette.enabled = false;
     }
 }

} And this is the second gameObject's showText script (IEnumerator, nor InvokeRepeating doesn not start onEnabled): using UnityEngine; using System.Collections;

namespace UnityStandardAssets.ImageEffects { public class ShowText2 : MonoBehaviour {

     public SpriteRenderer Dialog5;
     public SpriteRenderer Dialog6;
     public SpriteRenderer Dialog7;

     public GameObject PLAYER;
     public Rigidbody2D pl_Rigidbody2D;
     public VignetteAndChromaticAberration vignette;

     public RigidbodyConstraints2D constraints;
     public int timerSeconds = 60;


     void CountDown()
     {

         timerSeconds--;

         if (timerSeconds < 1)
         {
             Dialog5.enabled = true;
             pl_Rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;
         }

         if (timerSeconds < 5)
         {
             Dialog5.enabled = false;
             Dialog6.enabled = true;
         }

         if (timerSeconds < 6)
         {
             Dialog6.enabled = false;
             Dialog7.enabled = true;
         }

         if (timerSeconds < 6)
         {
             Dialog7.enabled = false;
             pl_Rigidbody2D.constraints = RigidbodyConstraints2D.None;
             vignette.enabled = false;

             CancelInvoke("CountDown");
         }
     }

     void OnEnabled()
     {
         InvokeRepeating("Countdown", 1, 1);

         Dialog6.enabled = false;
         Dialog7.enabled = false;

         pl_Rigidbody2D = PLAYER.GetComponent<Rigidbody2D>();
         vignette = Camera.main.GetComponent<VignetteAndChromaticAberration>();
         vignette.enabled = true;

         Destroy(gameObject, 19);


         CountDown();
     }
 }

Here is the part on the player which activates second game object: void Update () { if (curRunes == 1 && Odin2 !=null) { wunjo = true; Odin2.SetActive(true); } Please help me find a solution where courutine works after gameObject gets enabled!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vintar · Dec 06, 2015 at 10:07 PM

Its a typo, my speciality :) it should be OnEnable()

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 AttilaZold · Dec 07, 2015 at 07:54 PM 0
Share

Thanks, but I've tried OnEnable() and the second gameObject doesn't appear when player's conditions are met. It only becomes active if I write OnEnabled()... so it must be something else that messes with the Counter.

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

31 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

Related Questions

Execution Order of Scripts - Awake and OnEnable 1 Answer

A way to know if unity was just opened? [ExecuteInEditMode] 0 Answers

C# Children of GameObject has it's script disabled after SetActive(false) and SetActive(true) 1 Answer

My raycast wont function properly 0 Answers

How to create a score manager script involving awarding points from multiple objects? 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