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
3
Question by microp · Apr 24, 2011 at 03:02 AM · instantiategeneric

Object.Instantiate. , really?

Hey!

The Unity script reference mentions what looks like a C# generics version of Object.Instantiate(), Object.Instantiate.<> . Is this a typo? Is this a C# syntax I don't know about? In any case, I can't get a generic version to work in my C# script. Is the only option to cast the result into the desired type?

Many thanks!

Comment
Add comment · Show 1
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 microp · Apr 24, 2011 at 05:37 PM 0
Share

Argh, threee excellent answers! Which one should I pick, which one should I pick?? Guess I'll go with Jessy's just to even reputations out a bit. :P

4 Replies

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

Answer by Jessy · Apr 24, 2011 at 05:27 AM

It does appear to be broken. It should look like this:

Instantiate<GameObject>(prefab);

But the closest you could get right now would be one of these

this.Instantiate<GameObject>(prefab);   
prefab.Instantiate();

You would need a class with extension methods, for that:

using UnityEngine;

public static class ExtensionMethods {

public static T Instantiate <T> (this Object unityObject, T t) where T : Object { return Object.Instantiate(t) as T; }

public static T Instantiate<T>(this T unityObject) where T:Object { return Object.Instantiate(unityObject) as T; }

}

This is nowhere near as handy as the generic GetComponent, and seems pointless to me, especially given that it will carry overhead. I am interested to know if a generic Instantiate can be faster than using as, if Unity Technologies implements it.

Comment
Add comment · Show 6 · 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 Statement · Apr 24, 2011 at 05:48 AM 1
Share

The generic GetComponent also has overhead and looks similar to your implemenetation. In fact, they are calling GetComponent(typeof) as T; (or I can't remember if they used the cast operator. The strange bottom line is that I managed to do an exact similar GetComponent method with less overhead than the built in one. I was quite surprised! However, the price we pay is rather small. A typeof, and a cast.

avatar image Jessy · Apr 24, 2011 at 06:04 AM 0
Share

How do you know what it looks like? Reflection? Here's some further info on that in case anyone finds Statement's post informative: http://forum.unity3d.com/threads/85229-can-you-use-AddComponent-with-a-custom-script?p=548746&viewfull=1#post548746

avatar image dissidently · Apr 24, 2011 at 07:02 AM 0
Share

oh. three of the four great $$anonymous$$ds of Answers have found this, yet it's not fixed/corrected.

avatar image Statement · Apr 24, 2011 at 07:47 AM 0
Share

Yes Jessy, I have replied to another question with more details about it, but I couldn't find it. I'd link it if I had. I tend to mess around with Reflector from time to time to build a better understanding of the engine.

avatar image microp · Apr 24, 2011 at 05:44 PM 0
Share

Right. What bothers me is that explicit cast and I thought I could get around it somehow. Oh well, not that big of a deal performance-wise anyway. If the return value ends up being cast anyway, I'd rather have it in my face than hidden behind a sexy function. Thanks everyone!

Show more comments
avatar image
1

Answer by Eric5h5 · Apr 24, 2011 at 03:59 AM

It's JS syntax, like the generic version of GetComponent. I could never get it to work in either JS or C# though, so I have to wonder if it's actually implemented.

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 microp · Apr 24, 2011 at 05:38 PM 0
Share

Interesting. I'll keep that in $$anonymous$$d, thanks!

avatar image
1

Answer by Statement · Apr 24, 2011 at 07:55 AM

The UnityEngine.dll doesn't even have a generic method for that. I checked it out with Reflector just now (Unity version 3.3.0f4).

  • My guess is that it never made it into the final build somehow.

Even the docs for it seem a bit forgotton about. There isn't even a description for it. I'd disregard it.

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 microp · Apr 24, 2011 at 05:46 PM 0
Share

Yeah that whole part of the docs seemed fishy to me, hence my question. Thanks!

avatar image
0

Answer by Jeff-Kesselman · Sep 24, 2020 at 06:56 PM

AIR Once upon a time there was a T Instantiate<T>()

where T was a MonoBehavior. It would create a new game object, create the passed type of MonoBehavior ON that game object, and return a ref to the new MonoBehavior.

My best guess is that they eliminated that function but missed the reference to it in the docs.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Instantiating GameObject from Generic Class 1 Answer

Get method in script with Generic list 1 Answer

Checking if object intersects? 1 Answer

Changing LocalScale in code affecting prefab stored in Assets 1 Answer

Create a new object after the collision of two others 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