Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kurai · Aug 13, 2013 at 08:37 AM · transformassetresources

Can't load prefabs from resources into an array

I'm making a game with very small levels (think Wario Ware Style). I decided that every microlevel should be a prefab. So I'm going to put all the level prefabs into a Levels directory inside my Resources folder. Then I'm planning to load all the levels into an array, shuffle it with a simple algorithm and then spawn one of them at a time. (I've got an external manager calling the spawn function when needed). Problem is, it seems not to load my levels at all. Is there something wrong in what I'm doing? And plus, is this a good approach? Thank you!

     private Transform _currentLevel;
     private Transform _player;
     private Transform[] _levels;
     private int _levelIndex;
     
     // Use this for initialization
     void Start () {
         _player = GameObject.FindGameObjectWithTag("Player").transform;
         //load all levels from resources
         _levels = Resources.LoadAll("Levels") as Transform[];
         _levelIndex=0;
     }
     
     public void SpawnLevel ()
     {
         if (_levelIndex==_levels.Length-1)
         {
             _levelIndex=0;
             MixLevels ();
         }
         
         _currentLevel=Instantiate(_levels[_levelIndex],transform.position,Quaternion.identity) as Transform;
         _currentLevel.parent=this.transform;
         _player.position=new Vector3(0,1,0);
         _player.gameObject.SetActive(true);
         
         _levelIndex++;
     }
     
     public void DestroyLevel ()
     {
         _player.gameObject.SetActive(false);
         if (_currentLevel) Destroy(_currentLevel.gameObject);
     }
     
     public void MixLevels()
     {
         //apply a Fisher-Yates algorithm to create a new level pool
         for (int i = _levels.Length-1; i>0; i--)
         {
             var r = Random.Range(0,i);
             var tmp = _levels[i];
             _levels[i]=_levels[r];
             _levels[r]=tmp;
         }
     }
 }
 
Comment
Add comment · Show 7
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 Joyrider · Aug 13, 2013 at 08:45 AM 0
Share

Your array stays empty?

avatar image kurai · Aug 13, 2013 at 08:49 AM 0
Share

Looks like that. For example, if I do a Debug.Log(_levels[0].name); I get a NullReferenceException error. If I do a Debug.Log(_levels) I get a Null. The array looks empty to me :/

avatar image vexe · Aug 13, 2013 at 09:26 AM 0
Share

So obviously you're not getting anything back from Resource.LoadAll, have you tried loading each level individually in a loop? levels[i] = Resource.Load("Level" + i) as Transform;

I wouldn't go for this approach, why loading everything all at once and then use whenever you need? - just load whenever you need, I would use a yield return 'level_I_need' for that.

avatar image kurai · Aug 13, 2013 at 09:32 AM 0
Share

The problem with loading just one level at a time is that way I have to know exactly how many microlevels are in the game. What I need is something that could adapt if I decide to add new levels. By loading all of them I'm sure I always get the array with the right dimension.

Actually, the idea of loading everything from resources was just to let me add new levels as I'm working, without having to resize arrays or add them manually to a list everytime I create a new one :)

BTW what exactly do you mean to use a yield for that? I'm confused, can you elaborate? Thanks!

avatar image Joyrider · Aug 13, 2013 at 09:40 AM 0
Share

And if you try the load without casting to Transform[]? and save them in an Object[] array? Does it still stay empty?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Joyrider · Aug 13, 2013 at 09:43 AM

This should work :

Object[] _levelObj = Resources.LoadAll("Levels", typeof(Object));

my guess is, Unity doesn't like your array casting as Transform[]

Comment
Add comment · Show 4 · 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 kurai · Aug 13, 2013 at 09:58 AM 0
Share

Oh, it seems the casting was the problem. Now it works (almost) perfectly. I've still got a problem with the instantiate part, it correctly spawns the level, but somewhat it cannot parent it to my gameobject (it throws the same Nullreference Exception). Let's see if I can solve this.

avatar image vexe · Aug 13, 2013 at 10:20 AM 0
Share

'what' is null? this.transform?

avatar image vexe · Aug 13, 2013 at 10:23 AM 1
Share

After you loaded your levels as objects, did you convert each one again to 'Transform'?

avatar image kurai · Aug 13, 2013 at 10:40 AM 0
Share

Solved, still a transform casting problem. I casted to a GameObject ins$$anonymous$$d and everything works now, thanks!

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

17 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

Related Questions

project window show very small thumbs for assets. 1 Answer

Alternatives to Resources.Load or Refining that command 1 Answer

Loading directories and custom files 0 Answers

Obtaining the relative size of a Models body part 1 Answer

Loading/mounting alot of assetbundles 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