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 YazanDeAce · Aug 30, 2016 at 03:01 PM · instantiatecoroutineassetbundlecoroutinesinstantiate-objects

Instantiated object from asset bundle is null

Hello, I am instantiating an object from asset bundle. That works fine, but when I try to check if the object's prefab is not null to call another function, it returns null.. Here's the code

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 namespace RoboGarden
 {
     public class CollectiveObject : FieldObject
     {
         public CollectiveObject(int x, int y, string name,string type, float width = MainController.CELL_SIZE, float height = MainController.CELL_SIZE) : base(x, y, name, "collective", type, width, height)
         { }
         public GameObject logo = GameObject.FindGameObjectWithTag("logo");
         AssetBundleRequest request;
         MonoBehaviour temp = GameObject.FindObjectOfType<MonoBehaviour>();
 
         public IEnumerator ObjectCreator()
         {
             LoadingMenu myAssets = logo.GetComponent<LoadingMenu> ();
             request = myAssets.wwwww.assetBundle.LoadAssetAsync (type, typeof(GameObject));
 
             yield return request;
             GameObject objj = request.asset as GameObject;
             objj.transform.localScale = new Vector3 (MainController.SCALE * MainController.CELL_SIZE / realW, MainController.SCALE * MainController.CELL_SIZE / ((realW + realH) / 2), MainController.SCALE * MainController.CELL_SIZE / realH);
             float X = (xIndex * MainController.SCALE * objectWidth) - (MainController.SCALE * objectWidth / 2);
             float Y = (yIndex * MainController.SCALE * objectHeight) - (MainController.SCALE * objectHeight / 2);
             obj = UnityEngine.Object.Instantiate(objj, new Vector3(X, 0, Y), Quaternion.identity) as GameObject;
             obj.name = objectName;
 
         }
         public bool inti()
         {
                 temp.StartCoroutine(ObjectCreator ());
 
             if (obj != null) {
                 Debug.Log ("Found it !");
                 return true;
             }
             else {
                 HelperFunctions.consolePrint(type + " is to found in reasouces");
                 return false;            
             }
 
             }
     
         }
     }
   


Edit: I figured that Coroutines are causing it, the check happens before coroutine finishes. Why? What am I doing wrong?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 namespace RoboGarden
 {
     public class CollectiveObject : FieldObject
     {
         public CollectiveObject(int x, int y, string name,string type, float width = MainController.CELL_SIZE, float height = MainController.CELL_SIZE) : base(x, y, name, "collective", type, width, height)
         { }
         public GameObject logo = GameObject.FindGameObjectWithTag("logo");
         AssetBundleRequest request;
         MonoBehaviour temp = GameObject.FindObjectOfType<MonoBehaviour>();
 
         public IEnumerator ObjectCreator()
         {
         
             LoadingMenu myAssets = logo.GetComponent<LoadingMenu> ();
             request = myAssets.wwwww.assetBundle.LoadAssetAsync (type + ".prefab", typeof(GameObject));
 
             while(!request.isDone) {
                 yield return new WaitForSeconds (2f);
             }
             
             GameObject objj = request.asset as GameObject;
             obj = GameObject.Instantiate (objj) as GameObject;
             obj.transform.localScale = new Vector3 (MainController.SCALE * MainController.CELL_SIZE / realW, MainController.SCALE * MainController.CELL_SIZE / ((realW + realH) / 2), MainController.SCALE * MainController.CELL_SIZE / realH);
                 float X = (xIndex * MainController.SCALE * objectWidth) - (MainController.SCALE * objectWidth / 2);
                 float Y = (yIndex * MainController.SCALE * objectHeight) - (MainController.SCALE * objectHeight / 2);
             obj.transform.position = new Vector3 (X, 0, Y);
             obj.name = objectName;
             yield return obj;
 
             }
             
         public IEnumerator DummyFunction()
         {
             yield return temp.StartCoroutine (ObjectCreator ());
 
         }
 
         public bool inti()
         {
 
             temp.StartCoroutine (DummyFunction ());
             
             if (obj != null) {
                 Debug.Log ("Found it!");
 
                 return true;
         } else {
             HelperFunctions.consolePrint ("Not found");
             return 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 Bonfire-Boy · Aug 31, 2016 at 12:30 AM 0
Share

If you need the coroutine to have finished before you move on to the next thing, then you need to wait for it to finish. It looks to me like you're just starting it (on line 45) and then immediately checking the value of obj, which the coroutine won't have set yet (because there are yields beforehand). There are several possible ways of doing this. You could for example have the coroutine itself end by kicking off whatever has to happen next. Or another way would be to call the coroutine from another coroutine using yield return StartCoroutine(), so that the outer one waits for the inner one to finish before moving on.

avatar image YazanDeAce Bonfire-Boy · Aug 31, 2016 at 03:12 PM 0
Share

Thing is I cannot set this bool inti() to IEnumerator, because another script depends on its returned bool value. I tried IEnumerator , but when I try to write an if condition that checks the returned value, it tells me that I cannot convert Coroutine to bool..

avatar image Bonfire-Boy YazanDeAce · Aug 31, 2016 at 03:36 PM 1
Share

That's right, you can't return a value from a coroutine like that. There are other ways to achieve what you're after though.

For example, your coroutine could set a boolean field in the class which the subsequent code can then check ins$$anonymous$$d of using a return value. This can work in simple cases but it has the drawback that all calls to the coroutine share the same flag.

A more robust and flexible approach is to pass a callback function (with a single bool parameter) to the coroutine, and then when the coroutine has finished, call that function passing in the bool that you want to "return" from the function. Your callback function contains the code you want to execute next.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Wait for coroutine to finish before returning a value in another function 0 Answers

Issue with nested coroutines getting stuck 1 Answer

Why doesnt my coroutine ever end? 2 Answers

StopAllCoroutines not working 0 Answers

How do I instantiate the contents of an asset bundle multiple times? 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