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 Herzleiid · Aug 21, 2017 at 08:02 PM · gameobjectcoroutinecoroutine errors

Flawed code?

Hello!

First of all, "prefabConst" is a prefab for a construction building sprite, and "prefab" is the actual, finished building.

So my issue is that I have a quite simple code which after a button has been clicked, the event system runs a public function Build (). Everything works flawlessly until the Construction coroutine. Sometimes and for no apparent reason (and I repeat, ONLY sometimes) it doesn't run. I'm not sure if it doesn't run but the construction sprite persists which only tells me it didn't run in the first place.

I failed to find any patterns in why it decides not to work (like clicking too fast).

Does anybody have an idea on what could I do about this?

     public void Build () {
         x = Random.Range (-4.5f, 4.5f);
         y = Random.Range (-2.5f, 2.5f);
         z = 0;
         pos = new Vector3 (x, y, z);
         GameObject storeMe = Instantiate (prefabConst, pos, Quaternion.identity);
         StartCoroutine (Construction(storeMe));
 }
 
     IEnumerator Construction (GameObject destroyMe) {
         yield return new WaitForSeconds (5);
         Instantiate (prefab, destroyMe.transform.position, Quaternion.identity);
         Destroy (destroyMe);
     }
Comment
Add comment · Show 11
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 Xarbrough · Aug 21, 2017 at 09:27 PM 0
Share

Nothing I can see in the posted code. Have you tried isolating the issue? $$anonymous$$ake a backup of your scene and delete everything unrelated or try to reproduce the issue in a clean scene. One issue might of course be, that you might be starting multiple coroutines after one another. Call StopAllCoroutines() before starting a new one to check for that.

avatar image Herzleiid Xarbrough · Aug 22, 2017 at 11:51 AM 0
Share

The problem with stopping the couroutines is that I won't be able to build multiple buildings. If I were to spam the Build() function the construction sprites would persist. right?

avatar image TheSOULDev · Aug 21, 2017 at 11:37 PM 0
Share

The only thing which could be a problem here is if you're passing store$$anonymous$$e before it's actually loaded. In other words, if you're passing an argument to your Coroutine somehow after it has been set to something other than null, but not fully loaded. I'd suggest you make Build an IEnumerator, run it as a coroutine, and yield return new WaitForSeconds(1f) after the store$$anonymous$$e initialization. If it's not that, I really don't see where the problem would be.

avatar image Herzleiid TheSOULDev · Aug 22, 2017 at 11:59 AM 0
Share
     public void Build () {
             StartCoroutine ("PreConstruction");
         }
 
     IEnumerator PreConstruction () {
         x = Random.Range (-4.5f, 4.5f);
         y = Random.Range (-2.5f, 2.5f);
         z = 0;
         pos = new Vector3 (x, y, z);
         GameObject store$$anonymous$$e = Instantiate (prefabConst, pos, Quaternion.identity);
         yield return new WaitForSeconds (1);
         StartCoroutine (Construction (store$$anonymous$$e));
     }
 
     IEnumerator Construction (GameObject destroy$$anonymous$$e) {
         yield return new WaitForSeconds (5);
         Instantiate (prefab, destroy$$anonymous$$e.transform.position, Quaternion.identity);
         Destroy (destroy$$anonymous$$e);
     }

Here's what I did. Surprisingly this new script makes the bug quite a lot more frequent.

avatar image akillingbeck · Aug 22, 2017 at 11:54 AM 0
Share

Is this monobehaviour that the coroutine is running in getting disabled at any point?

avatar image Herzleiid akillingbeck · Aug 22, 2017 at 02:30 PM 0
Share

It isn't. It's stored inside the button that activates it but neither the GO or the Script itself are getting disabled at any point that I recall.

avatar image Herzleiid · Aug 22, 2017 at 12:07 PM 0
Share

After some debugging, I found out that whenever a Debug.log is running in the IEnumerator I can not reproduce the error. It's incredible, does this have some kind of explanation? It looks way too random to me but I keep trying to reproduce the error and it just won't happen, but as soon as I remove the Debug.log the error appears again.

avatar image Herzleiid Herzleiid · Aug 22, 2017 at 12:10 PM 0
Share

Actually, I was able to do it after alt + tabbing. $$anonymous$$ost likely it was probability. I refuse to believe alt tabbing unity does break the code.

avatar image DJT4NN3R · Aug 22, 2017 at 12:22 PM 0
Share

A slight optimization idea: It looks as though you're only using the instance of prefabConst as a marker object to tell the Construction Coroutine where to place the new building. If you have already stored the position where the marker object is instantiated at to the variables x, y, and z, why not just change your Construction coroutine's arguments to accept those variables as arguments and instantiate the new building using those arguments as the position? I think it's a little inefficient to create a GameObject for the purpose of storing data.

avatar image Herzleiid DJT4NN3R · Aug 22, 2017 at 02:29 PM 0
Share

The GameObject is created to show an "under construction" sprite, which after a while it is destroyed to show the finished building. Although I have been thinking about using the variable "pos" ins$$anonymous$$d of the GameObject, but I came to think it's just the same one way or another. Would it be better to use "pos"?

avatar image DJT4NN3R Herzleiid · Aug 22, 2017 at 02:50 PM 0
Share

Oh, no, that's fine then - I didn't know that there was a sprite attached to the marker object to convey that a building is currently under construction at that location. If it has functionality then, you should leave it be. :)

0 Replies

· Add your reply
  • Sort: 

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

100 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

Related Questions

Coroutine start in while 2 Answers

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Makes object appear and disappear after a few seconds 3 Answers

Enemy attack constantly with a coroutine 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