• 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
Question by FuryFight3r · Jun 07, 2017 at 08:46 AM · coroutinecoroutine errors

Coroutine Local Scale Stops Milliseconds After Starting

Hey there guys, I'm having an issue with a growing (Scaling) Coroutine that stops very very shortly after starting, I have a plant that starts at the size of 0.1 that grows to a size of 1.0, but as soon as the Coroutine starts, it seems to stop near instantly, the plant scales by 0.002, making the plant size 0.102 but then stops there, while i am still receiving the debug logs of...

'Growing True'

'Max Size is Higher...'

'Fertilizer not Added'

ect...

But the plant fails to scale.

Below is the code I currently have that is attached to an instantiated plant from a seed.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class growingScript : MonoBehaviour {
 
     public bool growing = true;
     public bool isGrown = false;
     public bool fertAdded = false;
 
     ParticleSystem fertParts;
 
     Transform dirt;
 
     public float growFactor = 0.1f;
     float startGrow;
     float fertMult;
     public float maxSize = 10;
 
     void Start()
     {
         dirt = gameObject.transform.parent;
         fertAdded = dirt.GetComponent<plantingScript>().fertAdded;
         startGrow = growFactor;
         fertMult = startGrow * 2;
         StartCoroutine(Scale());
     }
 
     void Update()
     {
         growing = true;
         fertAdded = dirt.GetComponent<plantingScript>().fertAdded;
     }
 
     public IEnumerator Scale()
     {
         Debug.Log("Coroutine started");
         while (growing)
         {
             Debug.Log("Growing True");
             while (maxSize >= transform.localScale.x)
             {
                 Debug.Log("Max Size Higher than Current Size, Growing");
                 gameObject.transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
                 while (fertAdded)
                 {
                     Debug.Log("Fertilizer Added");
                     growFactor = fertMult;
                     yield return null;
                 }
                 while (!fertAdded)
                 {
                     Debug.Log("Fertilizer not Added");
                     growFactor = startGrow;
                     yield return null;
                 }
                 yield return null;
             }
             while (maxSize <= transform.localScale.x)
             {
                 Debug.Log("Max Size Lower than Current Size, Stop Growing");
                 growing = false;
                 isGrown = true;
                 StopCoroutine(Scale());
                 gameObject.layer = LayerMask.NameToLayer("Plant");
                 yield return null;
             }
         }
     }
 
 }
 
Comment

People who like this

0 Show 0
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
Best Answer

Answer by FuryFight3r · Jun 09, 2017 at 09:02 PM

Issue Fixed!

     public IEnumerator Scale()
     {
         Debug.Log("Coroutine started");
         while (growing)
         {
             Debug.Log("Growing True");
             while (maxSize > transform.localScale.x)
             {
                 while (fertAdded)
                 {
                     growFactor = fertMult;
                     transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
                     yield return null;
                 }
                 while (!fertAdded)
                 {
                     growFactor = startGrow;
                     transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
                     yield return null;
                 }
                 yield return null;
             }
             while (maxSize < transform.localScale.x)
             {
                 growing = false;
                 isGrown = true;
                 StopCoroutine(Scale());
                 gameObject.layer = LayerMask.NameToLayer("Plant");
                 yield return null;
             }
         }
     }

Simply by moving the "transform.localScale" into the fertAdded while and !fertAdded while.

The issue I'm having now is that the plant keeps scaling and doesnt stop once it IS or is OVER the maxSize, but that will have to be asked in another question to avoid getting any naughty strikes on my account.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

68 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

Related Questions

I can't start a coroutine. I get a weird message from visual studio 1 Answer

Had difficulties implementing intro to Coroutines from unitypatterns.com. Help? 1 Answer

Coroutine sequence not running properly 1 Answer

Does UnityEngine.CustomeYieldInstruction works in a seperate thread? 2 Answers

Coroutine not working - what I did wrong? 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