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 Omti1990 · Dec 30, 2020 at 02:37 PM · instantiategenerics

Trying to create a generic functions for instantiations

Hello,
I've been trying to create a custom function for instantiating modules/scriptable objects.
The idea was that I'd input the "prefab" scriptable object and the object that was supposed to hold the instance of the scriptableObject.

 public static void CreateComponentInstance<T>(T _Type, T _instance) where T : Object
     {
         if (_Type == null) Debug.Log("No instance of _Type entered.");
         else _instance = Object.Instantiate(_Type);
     }

I would then execute this function like this:

 Util.CreateComponentInstance(mainDriveType,mainDrive);

The problem is after executing this, "mainDrive" is still null. And I don't really understand why. If I put an object into a function, I'm just transferring a reference, right? So if I change something with the reference, I should also change the original object? _instance isn't null after the instantiation, but "mainDrive" is.

What did I get wrong here?

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
1
Best Answer

Answer by Bunny83 · Dec 30, 2020 at 03:44 PM

Method parameters are always passed by value (== copied) unless you have a "ref" or "out" parameter. For more information see my answer over here.


Something like this would work:

 public static void CreateComponentInstance<T>(T aPrefab, ref T aInstance) where T : Object
 {
     if (aPrefab == null)
         Debug.Log("No prefab provided.");
     else
         aInstance = Object.Instantiate(aPrefab);
 }

I don't quite see the point of your method though. Your method name "CreateComponentInstance" also doesn't make much sense because that's not what that method does- All you do is creating a new object if the provided prefab / source object is not null. The newly created object is stored in the variable that is passed in by reference. So it's just the same as Instantiate but does log a message if no source object is provided-

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 sacredgeometry · Dec 30, 2020 at 07:32 PM 0
Share

Thats not true. Reference types passed into methods are passed by reference (albeit a copy of the reference).

The code below demonstrates that and outputs 0 then 12


     public class TestClass
     {
         public int Number { get; set; }
     }
 
     class Program
     {
         public static void ChangeNumber(TestClass testClass)
         {
             testClass.Number = 12;
         }
 
         static void $$anonymous$$ain(string[] args)
         {
             var test = new TestClass();
             Console.WriteLine(test.Number);
             
             ChangeNumber(test);
 
             Console.WriteLine(test.Number);
         }
     }
avatar image sacredgeometry · Dec 30, 2020 at 07:38 PM 0
Share

Oh wait getting confused. No you are right.

avatar image Bunny83 sacredgeometry · Dec 30, 2020 at 08:22 PM 1
Share

That's exactly the thing which seems to confuse most people. A lot seem to confuse the concept of a reference and the concept of a variable as well. A reference is simply put just an address. A variable is a memory location that can store a value (no matter if it's a value-type or reference type value).


A ref parameter is a direct pointer to the "variable". So changes to that parameter will affect the actual variable. Parameters that are not ref or out parameters are local variables to the method. They live in the stack frame of the method when the method is called.

avatar image sacredgeometry Bunny83 · Dec 30, 2020 at 08:25 PM 0
Share

Been working in c++ for too long I keep forgetting passing reference types into methods (without specifying ref) in c# is not analogous to just passing a reference in.

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

133 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

Related Questions

Checking if object intersects? 1 Answer

Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer

continuous shooting 1 Answer

[C#] How can I destroy instantiated prefabs when many are created with the same name? 2 Answers

Adding Imperfections? 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