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 primemover · Jan 06, 2014 at 06:26 AM · gameobjectprefabvariablesclassesresources.loadall

How do I access variables of prefabs I am loading via Resource.LoadAll, in script(C#)

I am having trouble retrieving variable information on some prefabs I am loading via resource.loadall.

I have a class called Tile that I have applied to some quad prefabs I have made in Unity, in it I have a few variables like displayName. They are public, and for each prefab I made and have in a resource folder I have a Display Name I have set in the Inspector.

I have these quads generating a tiled map, I basically use Resource.LoadAll to put all of the prefab tiles into an Object array, like so:

 Object[] prefabarray = Resources.LoadAll ("PrefabTiles/Primary", typeof(GameObject));

Just after this, I randomly pick one and have it instantiated into particular position, but before that I would like to retrieve or test what Display Name that random tile has, but when I try this:

 Debug.Log (prefabarray[0].displayName);

I get an error, and that var does not exist for that object. I am presuming I am missing a cast or something(you can see I am casting it as a GameObject above, don't know if that is correct, but debug.log tells me it IS a GameObject).

In game though, when I mouse-enter a certain tile, it will spit out displayName(I have it set up to do that). So in script I am just unable to get that info.

Any help is greatly appreciated, thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by GameVortex · Jan 06, 2014 at 08:53 AM

As you are saying displayName is not a variable of Object, but a variable of Tile. So you need to cast your list to another type. In the Resources.LoadAll you are not actually casting to GameObject, but you are telling it to load all GameObjects, the list returned will still be a Object[] list. To cast the list to GameObject you can do this:

 GameObject[] prefabArray = Resources.LoadAll("PrefabTiles/Primary", typeof(GameObject)) as GameObject[];

Notice I changed the type of the list as well as casting after the Resources.LoadAll function.

We still need to get a hold of displayname which is in the Tile component on those GameObjects. You can get a hold of components on GameObjects with the GetComponent function:

 Debug.Log (prefabarray[0].GetComponent<Tile>().displayName);

We can change some things around in the load section to make this even easier:

 Tile[] prefabArray = Resources.LoadAll("PrefabTiles/Primary", typeof(Tile)) as Tile[];

Here we are telling the function to load all GameObjects with a Tile component on it, we get a Object[] list then cast it to a Tile[] list and store it in the Tile[] prefabArray.

Then you we can do this to access the values:

 Debug.Log (prefabarray[0].displayName);
Comment
Add comment · Show 5 · 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 primemover · Jan 06, 2014 at 09:32 AM 0
Share

Thanks for your response, but when I use what you pasted(both) I get a NullReferenceException and an empty array.

Here is the code using Tile:

         GameObject[] prefabArray = Resources.LoadAll("PrefabTiles/Primary", typeof(GameObject)) as GameObject[];

         Debug.Log (prefabArray.Length);
            Debug.Log (prefabArray[0].displayName);
avatar image GameVortex · Jan 06, 2014 at 09:42 AM 0
Share

$$anonymous$$y bad, apparently you can not cast the Object[] into GameObject[] directly like that after all. But you can use this to specify the type ins$$anonymous$$d:

 GameObject[] prefabArray = Resources.LoadAll<GameObject>("PrefabTiles/Primary");

With this method there is no need to cast because the function returns a GameObject[].

avatar image primemover · Jan 07, 2014 at 03:02 AM 0
Share

Got it to work with a slight change. With what you gave me above I needed NikoBusiness's example of using GetComponent to get any of my Tile properties to be accessed, so I ended up just doing:

 Tile[] prefabArray = Resources.LoadAll<Tile>("PrefabTiles/Primary");

now I can get to those Tile properties directly.

Thanks!

avatar image primemover · Jan 07, 2014 at 03:04 AM 0
Share

Oh, and for future reference, what is that "< Tile >" part actually called, is that casting or something else?

avatar image GameVortex · Jan 07, 2014 at 09:05 AM 0
Share

I did show the use of GetComponent in my answer as well, but oh well. No matter. =P Glad I could help.

The arrow part is called a Generic Type Parameter. It is not casting, we supply the Type we are using and the function can then use that to return the correct type as well. It can not be used just anywhere though, the function needs to support it of course. And it is used for much more than only returning the correct type.

GetComponent uses it as well, and you can implement it yourself in functions and classes. You can read more about it **here**.

avatar image
0

Answer by NikoBusiness · Jan 06, 2014 at 10:28 AM

you have to use getComponent

 Debug.Log (prefabarray[0].GetComponent<ScriptName>.displayName);
 
 
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 primemover · Jan 07, 2014 at 03:03 AM 0
Share

Thanks Niko, this helped me figure out the solution that ended up working below.

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

20 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

Related Questions

Add 0.5f to Vector.Right on next instantiated object ? 1 Answer

How would I change the value of one prefab's variable via script without changing all of them? 1 Answer

Creating an array of prefabs? 4 Answers

Replacing empty gameobjects with prefabs 3 Answers

Sporadic Failure of GetComponentInChildren 0 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