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 Loius · Feb 18, 2013 at 08:00 PM · gameobjectcomponenttypeoverridegeneric

Is there a type which can refer to both GameObject and MonoBehaviour?

Or a way to trick one into existence?

I have this function (consider {} to be > and <):

public static void ProjectPicker{T}( ref T cur, Validator{T} validate ) where T : Component {

It builds a graphical interface to pick an object of type T from objects in the project (since Unity's ObjectField doesn't do any filtering at all).

I use the objects' parenting to sort them into folders, and as far as I know transform.parent is the only way to get the parent (right?). SO I need .transform, which both Component and MonoBehaviour have, but their parent (Object) does not.

Is there a way to write a single function that can accept both Component and MB as its T type and still access their .transform, or is there something I can do to write an override function of the other type and somehow call the original function (I'll do literally anything to not duplicate the function, including simply not support GameObjects at all), or is there another way to read an object's folder structure?

I'd like a single function, but if there's one like this I'd be fine:

 public static void ProjectPicker( ref GameObject cur, Validator{GameObject} validate ) {
   Component temp = ???;
   ProjectPicker( ref temp, validate );
   cur = (GameObject?)temp;
 }
Comment
Add comment · Show 2
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 Benproductions1 · Feb 19, 2013 at 12:28 AM 0
Share

cant you just go transform.parent as Transform ?

avatar image Loius · Feb 19, 2013 at 07:18 PM 0
Share

But I can't even get to .transform! This function searches prefabs for things matching the conditions in the validate function. I want to be able to say "Only search Projectile prefabs" or "Search all prefabs", and from what I can tell I would need to pass either Projectile or GameObject as the type T. Their common ancestor, Object, does not have a .transform member, so I have to find some way to convert from one to the other.

If i use where T : UnityEngine.Object, I can convert to GameObject, but I can't figure out how to convert to a Component descendent so I can send t through the validate function:

 T t;
 GameObject go, currentObject;
 ...
 go = currentObject as GameObject;
 if ( typeof(T) == typeof(GameObject) ) t = go as T;
 else t = go.GetComponent{T}(); // "T must descend from Component"

 if ( validate(t) ) { AddToPicker(); }

I need to get a component of type T, but T needs to be ensured to be a descendent of Component. I need an intermediary type:

 Type x = typeof(T) as Component; // but this doesn't compile
 t = go.GetComponent{x}();

1 Reply

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

Answer by Loius · Feb 19, 2013 at 07:26 PM

Transform is a Component, and every GameObject is guaranteed to have a Transform, so you can use Transform as your T type when you need to find every GameObject.

Derp.

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

10 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

Related Questions

Error CS0425: Works in DLL but not in Unity 0 Answers

Sharing components amongst different gameobjects 1 Answer

Determine if game object is tyoepf One Prefab 3 Answers

How to get the type of a Gameobject 3 Answers

Adding a script component through scripting 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