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
1
Question by saldavonschwartz · Apr 23, 2013 at 05:51 AM · c#prefabmonodevelop

how can I create an array or IList of a specific prefab type in C# ?

So, in theory all prefabs are of type GameObject. So how would I go about creating an array or IList that can only hold instances of specific prefabs?

That is, say I have a Node prefab with geometry and a NodeMechanics script attached.

I want Node prefabs to have (in the script) the properties

  • IListpreviousNodes

and

  • IListnextNodes

So that I can attach more node prefabs. If the lists where of type GameObject, any GameObject could be inserted, which I don't want.

I also thought of going for a Node script instead of NodeMechanics, so that the script itself, being a class, goes on to act as the "type" of the prefab. Though I think this is kind of against the Unity semantics or at least idiom, where a script seems to be thought of as one more component of the prefab?

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

1 Reply

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

Answer by whydoidoit · Apr 23, 2013 at 06:12 AM

You can just use List< YourScript>;

  List<YourScript> prefabs = new List<YourScript>();

When you instantiate you will need to cast the result to YourScript.

Prefabs are GameObjects, not a thing derived from them because this is a composition model not an inheritance one.

Comment
Add comment · Show 14 · 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 saldavonschwartz · Apr 23, 2013 at 06:20 AM 0
Share

right but what about this playing coherently with Instantiate()? say (for a Node prefab):

Instantiate(Resources.Load("Node"), transform.position, Quaternion.identity) returns a generic Object, which is not even the script type. What I'm wondering is if for a Node prefab with a Node$$anonymous$$echanics script attached, there is a way for me to statically type a list or array of only those prefab types. For your suggestion to work, I believe I'd have to still cast the Object to GameObject after Instantiate() and then further do a GetComponent() and then assign that component to the list / array. Further, the approach begs the question: should I even think of prefab names as anything other than a convenience identifier? $$anonymous$$ight as well rename the script to Node ins$$anonymous$$d of Node$$anonymous$$echanics and think of it as representing the type of the prefab?

avatar image whydoidoit · Apr 23, 2013 at 06:34 AM 0
Share

You cannot cast a GameObject to a script this is a composition model - you have to call GetComponent< YourScript>();

If you make that array of prefabs then instantiating them will return your script. You didn't mention you are using Resources.Load, which in fact should only be used in rare circumstances - you'd be better off having an object in the scene that contains your prefabs. Resources cause lots of challenges when making bigger games - like making it hard to create Asset Bundles that are efficient etc.

If you want to continue to use Resources.Load then you would need to make your line:

     prefabs.Add(  (Resources.Load("Node", typeof(GameObject)), transform.position, Quaternion.identity) as GameObject).GetComponent<YourScript>());
avatar image whydoidoit · Apr 23, 2013 at 06:36 AM 0
Share

If you have a scene object that contains a list, or multiple types of YourScript variables then:

    prefabs.Add(Instantiate(yourPrefabVariable, transform.position, Quaternion.identity) as YourScript);

Because the result of the instantiate will be YourScript but it's typed as UnityEngine.Object

avatar image saldavonschwartz · Apr 23, 2013 at 07:22 AM 1
Share

yeah it returns Object anyway. what DOES work though...

 Instantiate(Resources.Load("Node", typeof(Node$$anonymous$$echanics)), transform.position, Quaternion.identity) as Node$$anonymous$$echanics;
avatar image whydoidoit · Apr 23, 2013 at 07:30 AM 1
Share

Really consider not using Resources - they're a total disaster in my book. I just took delivery of a component for my game from a third party and my build increased by 100$$anonymous$$B because they'd put everything in a resources folder (including a picture of one guy's wife, a bunch of stuff they once used but upgraded etc - go figure) even though they only loaded 2 components from it at runtime.

Show more comments

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

13 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

Related Questions

Filling a scene with prefabs programmatically 2 Answers

MonoDevelop 4.0.1 code completion doesn't work 2 Answers

Is there a way to set components in a prefab before it is Instantiated? 0 Answers

How do I solve the unknown values in an equation? 0 Answers

Only instantiate if clear space beside prefab 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