Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:33:07+00:00 2026-06-12T07:33:07+00:00

In Unity csharp , I want to make a GetOrAddComponent method, which will simplify

  • 0

In Unity csharp, I want to make a GetOrAddComponent method, which will simplify the respectives GetComponent and AddComponent (for no good reason I suppose).

The usual way is this:

// this is just for illustrating a context
using UnityEngine;
class whatever : MonoBehavior {
public Transform child;
void whateverMethod () {

    BoxCollider boxCollider = child.GetComponent<BoxCollider>();
    if (boxCollider == null) {
        boxCollider = child.gameObject.AddComponent<BoxCollider>();
    }

}}

Right now I could make this class . . . :

public class MyMonoBehaviour : MonoBehaviour {

    static public Component GetOrAddComponent (Transform child, System.Type type) {
        Component result = child.GetComponent(type);
        if (result == null) {
            result = child.gameObject.AddComponent(type);
        }
        return result;
    }

}

. . . So this works:

// class whatever : MyMonoBehavior {

BoxCollider boxCollider = GetOrAddComponent(child, typeof(BoxCollider)) as BoxCollider;

But I wish I could write it like this:

BoxCollider boxCollider = child.GetOrAddComponent<BoxCollider>();

The only idea I could come up with would be way too complicated to do it (replacing each Transform with a MyTransform) and thus not worth the trouble of even trying. At least not just for a nicer syntax.

But is it? Or is there any other way this could be achieved?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:33:08+00:00Added an answer on June 12, 2026 at 7:33 am

    Have you tried using Extension Methods? You can declare them like this:

    public static class MyMonoExtensions {
    
        public static T GetOrAddComponent<T>(this Transform child) where T: Component {
            T result = child.GetComponent<T>();
            if (result == null) {
                result = child.gameObject.AddComponent<T>();
            }
            return result;
        }
    
    }
    

    You can call it like an instance method, then:

    child.GetOrAddComponent<BoxCollider>();
    

    See the link above for more details on extension methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Xbox-Unity and am trying to make a Kinect game. I need
Working with Unity, I thought it would be a good idea to create some
I'm currently using Unity with Unity.Mvc3 in my MVC3 application which uses Windows Authentication.
I'm doing a Unity-iPhone project, but want to automatically add some native code and
I have a 3D unity vector pointing in a certain direction. Now I want
How can I make Unity not to throw ResolutionFailedException if Resolve fails? Is there
I want to use Unity interception for an interface without an implementation, so that
In my Unity project I have 3 visual studio projects: Assembly-CSharp-vs Assembly-CSharp-firstpass-vs Assembly-CSharp-Editor-vs All
I want configure Unity to resolve an interface, say ITest , to a struct,
Unity's documentation says of the RegisterInstance<> method that registers an instance so that that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.