• 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 sadowlight123 · Dec 21, 2017 at 02:29 PM · unity 5functiongeneric

Generic Method help

Hello all , so I am trying to make a game and I need this piece of code:

 GameObject obj = (GameObject)Instantiate(pooledObject);
 obj.SetActive(false);
 pooledObjects.Add(obj);

this code is repeated more than one time , so I thought of putting it into a function (AddItem) However, in some cases , i need to use the obj variable and some others not so I need to sometimes return obj and function is as Gameobject and some other times I need is as void as it doesn't return anything. I recall learning something called templates in Advance Programming course in C++ After some search I found that It is referenced as Generic functions in C# / Unity

so far I got this :

 public T fillList<T>(T param)
 {
     GameObject obj = (GameObject)Instantiate(pooledObject);
     obj.SetActive(false);
     pooledObjects.Add(obj);
    // return obj;
 }

now my problem is that return obj is giving me an error , i am very unfamiliar with the syntax and way of writing generic functions , how can I remove the param and return the obj when it is called as Gameobject?

Can anyone please help? Thanks for all in advance!

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 PizzaPie · Dec 21, 2017 at 03:03 PM 0
Share

Could you present one example for each case of use?

Anyway you definitely don't need a generic method for something like this. You could always return the object but choose not to use it or add a parameter to your method to return null. Or create 2 functions, or an overload (but parameters gotta be different for each). Can't tell you which of all is optimum for your occasion with that info but not a generic one.

Cheers.

1 Reply

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

Answer by TreyH · Dec 21, 2017 at 03:08 PM

In C#, you can call any method as though it were of type void.


 public GameObject AddItem()
 {
     GameObject obj = (GameObject)Instantiate(pooledObject);
     obj.SetActive(false);
     pooledObjects.Add(obj)
     return obj;
 }

You don't have to get the object returned by this, but you can if you want! Either of these will work:


 // As a void
 this.AddItem();
 
 // As a GameObject
 GameObject newItem = this.AddItem();
Comment
Add comment · Show 3 · 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 ShadyProductions · Dec 21, 2017 at 03:10 PM 1
Share

'this' is redundant. But this is indeed the most correct answer.

avatar image TreyH ShadyProductions · Dec 21, 2017 at 03:13 PM 1
Share

It's sort of a habit, I've never liked seeing a naked function without class name for static or this for instance calls. :-(

avatar image Paulo-Henrique025 ShadyProductions · Dec 21, 2017 at 03:25 PM 1
Share

It is redundant indeed, but when posting small chunks it is nice to indicate class scope.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

146 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I use GetComponents reference outside Of awake and start functions 1 Answer

Function rerun when turning back on from the background 0 Answers

C# UI component overflaping another gameobject function 0 Answers

How to do x^-1 1 Answer

Function is not updating the variable 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