Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I select a random method?

Discussion in 'Scripting' started by Richard_Ingalls, Dec 10, 2022.

  1. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    I want to select a random method from several methods. At first I tried to use lists, but I cannot find how you would write that. This is basically the code I have:
    Code (CSharp):
    1. public delegate void Delegate1();
    2. public delegate void Delegate2();
    3. int index1;
    4. int index2;
    5. delegate void Method1();
    6. delegate void Method2();
    7. Method1 method1;
    8. Method2 method2;
    9. void Start()
    10. {
    11.    methods1 = new List<Delegate1>();
    12.    methods1.Add(ProjectileSizeUp);
    13.    methods1.Add(ProjectileSpeedUp);
    14.    methods1.Add(ProjectileNumberUp);
    15.    methods2 = new List<Delegate2>();
    16.    methods2.Add(ProjectileSizeDown);
    17.    methods2.Add(ProjectileSpeedDown);
    18.    methods2.Add(ProjectileNumberDown);
    19.    RandomButton0();
    20. }
    21. void ButtonFunc0()
    22. {
    23.    method1();
    24.    method2();
    25. }
    26. void RandomButton0()
    27. {
    28.    index1 = Random.Range(0, methods1.Count);
    29.    index2 = Random.Range(0, methods2.Count);
    30.    method1 = methods1[index1]();
    31.    method2 = methods2[index2]();
    32. }
    I have no idea how I would do this. Some of the random functions change a float and some set an object to active. Help!

    ps. If more information is needed, let me know.
    pps. If this is the wrong forum for this please let me know. I was recommended to come here from unity answers because no one would answer my question there so hopefully someone will here.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    ......

    This might be an advanced topic.

    You can write a lambda function.
    Code (csharp):
    1.  
    2.  
    3. using System;
    4.                  
    5. public class Program
    6. {
    7.     public static void Main()
    8.     {
    9.         System.Action method1;
    10.      
    11.         int test = 2;
    12.         method1 = () => {
    13.             test = 7;
    14.         };
    15.      
    16.         Console.WriteLine($"{test}");
    17.         method1();
    18.         Console.WriteLine($"{test}");
    19.     }
    20. }
    21.  
    22. .....
    23.  
    24. method1 = () => {
    25.     gameObject.SetActive(false);
    26. }
    27.  
    Or (pure C#):
    Code (csharp):
    1.  
    2. using System;
    3.                  
    4. public class Program
    5. {
    6.     public static void Main()
    7.     {
    8.         System.Action method1;
    9.      
    10.         int test = 2;
    11.         method1 = () => {
    12.             test = 7;
    13.         };
    14.      
    15.         Console.WriteLine($"{test}");
    16.         method1();
    17.         Console.WriteLine($"{test}");
    18.     }
    19. }
    20.  
    This
    () => {}
    form effectively creates an unnamed function which you can assign to delegates and System.Action.

    A lot of interesting things will happen if you populate the list and then manage to delegate the object function accesses before the function is called.

    Also see this thread for a detailed explanation:
    https://forum.unity.com/threads/sol...istener-events-in-batch.1137421/#post-7309777
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    The partial code you have looks basically fine. NegInfinity suggests you could _also_ write your functions directly inside the Add(), but writing them ahead of time -- like you did -- is completely normal. What isn't working?
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    ChatGPT (https://chat.openai.com/chat) generated around 90% of the following example. I made adjustments to the code (mostly removed the excessive comments it likes to generate) but otherwise I just fed it an explanation and it did the actual work.

    If you want to be able to pass methods to it from outside of the class you can just change the list to public.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ActionListExample : MonoBehaviour
    6. {
    7.     List<Action> actionList = new List<Action>();
    8.  
    9.     void Start()
    10.     {
    11.         actionList.Add(Method1);
    12.         actionList.Add(Method2);
    13.         actionList.Add(Method3);
    14.     }
    15.  
    16.     public void RunRandomMethod()
    17.     {
    18.         GetRandomMethod().Invoke();
    19.     }
    20.  
    21.     public Action GetRandomMethod()
    22.     {
    23.         return actionList[Random.Range(0, actionList.Count)];
    24.     }
    25.  
    26.     void Method1()
    27.     {
    28.         // do something
    29.     }
    30.  
    31.     void Method2()
    32.     {
    33.         // do something
    34.     }
    35.  
    36.     void Method3()
    37.     {
    38.         // do something
    39.     }
    40. }
     
    Last edited: Dec 11, 2022
  6. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Fascinating technique, tempting technique. Interesting technique ryiah.
     
  7. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    Ryiah, when I put in List<Action> it says that the type or namespace Action could not be found.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,844
    Action
    is part of the
    System
    namespace.

    So you'll need to include
    using System;
    at the top of your script with the rest of them, or namespace qualify your declaration with
    System.Action
    .
     
  9. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
  10. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    But officially, and in the wikipedia examples. the Strategy Pattern uses subsclassing, which is overly complicated. The examples always say to write an abstract class with a single overridable function. That's used as the plug. To create a function that plugs in, write a subclass and override that function with yours. And since Design Patterns are aimed at experienced OOP programmers, examples skip over how and why it works.

    Using
    System.Action callMe; ... callMe=myFunc1; ,,, callMe();
    is just way easier and more of the standard way now.
     
  11. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    No it uses a interface in C#
    A action is very generic and can create confusion in a large complex system.

    Edit: a action might be fine if the scope is small and private. Just make sure you don't capture any variables in the action. That will allocate
     
    Last edited: Dec 12, 2022
  12. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    How would I turn that into a list?
     
  13. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I'm surprised nobody mentioned Scriptable Objects, you can have a list of scriptable objects with a virtual method that you would invoke. I've been recommended to do like that when I implemented my UI behaviour, I think it's interesting to use them for random methods. You can drag them into a list from the inspector.
     
  14. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    What? You already did it. I'll explain how:

    Suppose we have just integers. We could write n1=3, n2=8, n3=12. That's 3 variables holding 3 numbers. If we had a List we'd use L.Add(3) L.Add(8) and L.add(12). Now we have a size-3 list holding 3 numbers. L[0] is like n1, L[1] is like n2 and L[z] is like n3.

    The same idea works for any type. We could have
    System.Action a1, a2, a3;
    and assign your functions:
    a1=projectileSizeUp;
    and
    a2=projectileSpeedUp;
    ... . Or we could do what you did and say
    methods.Add(projectileSpeedUp);
    and so on. That gives us a size-3 list like this:
    Code (CSharp):
    1. method[0] = projectileSizeUp;
    2. method[1] = projectileSpeedUp;
    3. method[2] = ...
    The only thing funny is you've got
    Delegate1
    as the type of the list. Up above you've defined it as a void(void) function (no input and no output). That's fine, but for one thing, it's a bad name. Why not use something descriptive like "bulletPowerUpAction"? It's also somewhat old C#. Most people would type
    Methods<System.Action>
    instead. System.Action just means "void(void) function" without someone having to look it up.
     
  15. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    I did that because I couldn't find any good information on anything concerning this topic. Thank you for pointing out how I already could use lists. I was tired at the time and was not thinking straight. But now I get a new error. It says "Cannot implicitly convert type 'void' to type 'System.Action'. Do you know why this could be?
     
  16. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    You did it correctly in your original code in
    methods1.Add(ProjectileSizeUp);
    . Look at exactly what's inside the ()'s for Add and what isn't there.

    We're taught that functions are always called with ()'s around the inputs. Even if it has no inputs the rule still applies -- call it as myFunc() with parens around all of the 0 inputs. But making references to _functions_ is a completely new thing. We want to be able to call it later, not now. Well, how in the heck do you write "no, I don't want to Add the result of calling this function. I want to add just the function"? And your original code is doing that.

    The thing is, references to functions is a tricky area, and using Lists is also a little tricky. I'm guessing you found a lot of this code somewhere and are in way over your head. You might try playing with just a List of integers (add to it, print items in it, print the whole thing, choose one at random ... ). Then play with normal function-references. Like
    System.Action<float,float,float> f;
    (f can be any function taking 2 floats and returning a float). Assign Untiy's Mathf.Min to it and try calling f(3,8). Then, in the same code, assign Mathf.Max to f and call f(3,8) again and see if it really gives you 8. Then write your own 2-input/1-output float function and see if f can handle that.
     
  17. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Code (csharp):
    1.  
    2.  
    3. public class GameController : MonoBehaviour
    4. {
    5.     public List<Methodologist> methods;
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         if (Input.GetKeyDown(KeyCode.Return))
    11.         {
    12.             applySomeEffect();
    13.         }
    14.      
    15.     }
    16.  
    17.     private void applySomeEffect()
    18.     {
    19.         var effectIndex = Random.Range(0, methods.Count);
    20.         methods[effectIndex].ApplyEffect();
    21.     }
    22. }
    23.  
    24.  
    Code (csharp):
    1.  
    2. public class Methodologist : ScriptableObject
    3. {
    4.     public virtual void ApplyEffect()
    5.     {
    6.         Debug.Log("apply this effect");
    7.     }
    8. }
    9.  
    Code (csharp):
    1.  
    2. [CreateAssetMenu(fileName = "Cold Effect", menuName = "Effect")]
    3. public class ColdEffect : Methodologist
    4. {
    5.     public int Intensity;
    6.  
    7.     public override void ApplyEffect()
    8.     {
    9.         Debug.Log($"That freezes for {Intensity} damage");
    10.     }
    11.  
    12. }
    13.  
    14.  
    Code (csharp):
    1.  
    2.  
    3. [CreateAssetMenu(fileName = "Fire Effect", menuName = "Effect")]
    4.  
    5. public class FireEffect : Methodologist
    6. {
    7.     public int Intensity;
    8.  
    9.     public override void ApplyEffect()
    10.     {
    11.         Debug.Log($"That burns for {Intensity} damage");
    12.     }
    13.  
    14. }
    15.  
    16.  
    upload_2022-12-15_4-37-51.png


    upload_2022-12-15_4-38-8.png

    upload_2022-12-15_4-38-38.png
     
  18. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    That scriptableObject approach is also exactly using the Strategy design pattern. It does a nice job of showing why it was invented. Clearly this way is overcomplicated: we had to write the extra Methodologist class, and invent an extra ApplyEffect function, and wrap our simple functions in that new class. Writing the two functions alone would be faster, easier to read, easier to test, and so on.

    But in this particular case we wanted to be able to drag-and-drop Assets into a list in the Inspector. Well, we can't really turn just a naked function into an asset ... the best we can do is put it in a script and put that script on an asset. But what's a good way to make the computer understand that plan? Oh -- a base class, inheritance and overriding (in other words, standard polymorphism).

    In other words, this does a nice job of showing how Strategy isn't a go-to trick, but it's a nice expert trick for the few times the go-to (simple C# delegates) won't work.
     
  19. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    This heavily breaks open / closed principle.

    https://en.wikipedia.org/wiki/Open–closed_principle
     
  20. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    How would I do that? It just says "Keyword "void" cannot be used in this context" when I try to do it with void instead of float. Do I need to do it as
    System.Action<void, void> f;
    ? Or am I missing something?
     
  21. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    void isn't a parameter, and thus shouldn't be placed inside the <>.
    Use:
    - System.Action f for methods without parameters (eg. DoSomething())
    - System.Action<float> f for methods with a single float-parameter (eg. DoSomething(float input))
    - System.Action<float, string> f for methods with a float-parameter AND a string-parameter (eg DoSomething(float input, string text))

    etc.
     
  22. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    The open-closed principle went out the window when OP wanted to 'invoke random methods'.
    If you want to use what @Ryiah posted, use custom delegates instead of Action to get cleaner code.
     
  23. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    uh? No. You can inject the list of strategies and execute a random one. Thats why strategies are nice since they work with open / closed. Though I sometimes break open closed too. But it should be a educated reason
     
  24. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    A 'list of strategies' is not the same as 'random methods'. That's why I pointed towards custom delegates.
    Whether you're injecting custom delegates or a custom interface, you're still adhering to the strategy pattern, as the implementation is 'forced to match' what the invoker is expecting.
     
  25. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    If you really want to use actions you can inject a list of actions and still not break open closed principle. Though most cases need instance data passed along the code to invoke and then a strategy is well suited. Since it can access instance variables without capturing
     
  26. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    I'm curious where this particular Open/Closed definition comes from, just as ComSci history. The original in the 90's was about "modifying" a class by making subclasses. A little later they said yes, it was still subclasses, but the base has to be fully abstract (in Java/C# terms, Open/Closed meant using an Interface instead of a base class). Wikipedia has those two ways, since that's the "real" definition. But then as OOP-all-the-time fell out of favor, people starting saying the point of Open/Closed was about never changing the original class, instead provide just any way to extend it. A subclass is one way, but plug-in functions is another better way (which is what the OP wrote).
     
  27. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    The orginal poster needs to add a new method plus adding that method to the list of actions. That breaks open closed. If you inject a list of methods into the class that does not break open closed.
     
  28. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    How would I use
    System.Action f
    as a list?
     
  29. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Code (CSharp):
    1. private void PickRandomAction()
    2. {
    3.    var list = new List<Action>();
    4.  
    5.    // Add a Lambda expression
    6.    list.Add(() => Debug.Log("Hello from Lambda"));
    7.  
    8.    // Add a Lambda function (multiline)
    9.    list.Add(() =>
    10.         {
    11.             var today = DateTime.Today;
    12.             Debug.Log($"Hello from (Multiline) Lambda. Today is {today}.");
    13.         });
    14.  
    15.    // Add a Method. Notice we didn't add () after the method name
    16.    list.Add(HelloFromMethod);
    17.  
    18.    // Let's pick a random action...
    19.    var randomIndex = Random.Range(0, list.Count);
    20.    var randomAction = list[randomIndex];
    21.  
    22.    // Invoke (execute) the action
    23.    randomAction.Invoke();
    24. }
    25.  
    26. private void HelloFromMethod()
    27. {
    28.     Debug.Log("Hello from Method");
    29. }
     
    Last edited: Jan 5, 2023
  30. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    When I do
    var list = new List<Action>();
    , it says there is no type "Action". And when I use
    var list = new List<System.Action>();
    it says "Cannot implicitly convert type "void" to type "Action""
     
  31. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    Advanced practices and principles are for experienced developers not people who are struggling to write a basic script. Based on the errors they're consistently struggling with this is the solution that they should go with if they just want to get the job done.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class RandomMethodSelector : MonoBehaviour
    4. {
    5.     public void SelectRandomMethod()
    6.     {
    7.         var r = Random.Range(0, 2);
    8.  
    9.         if (r == 0) MethodA();
    10.         if (r == 1) MethodB();
    11.         if (r == 2) MethodC();
    12.     }
    13.  
    14.     void MethodA()
    15.     {
    16.  
    17.     }
    18.  
    19.     void MethodB()
    20.     {
    21.  
    22.     }
    23.  
    24.     void MethodC()
    25.     {
    26.  
    27.     }
    28. }
     
    Last edited: Dec 21, 2022
    Nad_B likes this.
  32. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    This does not work because I have much more than just three methods, and I need the list's ability to remove an item from it because it will be calculated multiple times, without replacement. This solution becomes impractical when there are many methods.
     
  33. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    But once we give the class a public list of "random" methods, we never change it again (compared to Ryiah's sample code, where every new random function requires a change to the class's code). That's the definition of being closed, right? And it's "open to extension" since we can alter the contents of the list. That seems like the modern definition of Open/Closed, to me at least.

    Or is it about the injection? Some people say the only proper way to "inject" into a class is having it be in the constructor. But it's common to say injection is just any time the class doesn't need to "know about" the possible functions ahead of time -- some outsider assigns them.
     
  34. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    None of this applies to me. I am trying to figure out how to add methods to a list. I have the methods elsewhere in the script, and need to somehow pick three random methods without replacement. Sorry if I did not make this clearer earlier.
     
  35. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    Thats not how open closed work. You break open closed principle once you cant extend it without altering its code. Doesnt matter if you ever will edit that class again
     
  36. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    I somewhat agree with you, Im just pointing out the facts
     
  37. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Post your code.
     
  38. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    What code would be relevant? From what I can see, you have all the needed information, I have posted most of my code throughout this thread, just look at previous posts.
     
  39. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Really sorry about this, what was I thinking when I tried to help you! I promise it won't happen again. Can you forgive me?
     
  40. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    Huh? I was asking a legitimate question. If I am wrong, and haven't posted all needed information, then please tell me what would be needed.
     
  41. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    The thing is, the code you posted at first is pretty much correct. So everyone figured you understand C# lists and assigning functions to variables and were maybe missing 1 little thing. Someone (me) even asked you what the problem was (and never got an answer).

    Then it seems you're having trouble with more basic stuff. Like maybe you got that code from somewhere but don't know how it works. That's fine -- lots of people who come here are just starting to learn scripting. But it's confusing people as to how much you actually know, so you're getting odd replies.
     
  42. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    I apologize for not giving you an answer, I must have missed your question. I unfortunately do not understand how to assign a function to a variable, and I wrote the code I did based off the assumption that I could, with help from people like you, tweak it so that it worked. I just have received a lot of information that was inaccurate from other places, and I have had trouble conveying what I want.
     
  43. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Those are even more reasons to post your whole script, the whole thing. If there are multiple scripts involved, you should post them too. If you copied the code from some place, you should say where did you get it from. If it's too much code, you should post a smaller but complete example that you believe should work but doesn't.

    You should also post the complete errors that the console prints, because they say where exactly in the code is the problem. Make it easier for people to help you. Save yourself some time going back and for on what to share.

    EDIT
    That said, in your code from the first post, in lines 30 and 31, you should remove the parentheses. Look at it this way:
    Code (CSharp):
    1. //Suppose you have a method DoSomething:
    2. public void DoSomething()
    3. {
    4. }
    5.  
    6. // This stores DoSomething in a variable.
    7. System.Action myAction = DoSomething;
    8.  
    9. //This stores the result executing DoSomething in a variable.
    10. //Executing DoSomething returns void, and void can't be converted to System.Action.
    11. System.Action myAction = DoSomething();
     
    Last edited: Dec 23, 2022
  44. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    Oooh, ok. That makes sense. I am relatively new to coding, and even if I wasn't, there are many little things that you might never come across for a long while. I did eventually get it working by using:
    Code (CSharp):
    1. delegate void methodGrabber();
    2. List<methodGrabber> methods1;
    instead of what I was using before and changing some functions:
    Code (CSharp):
    1.     void method1()
    2.     {
    3.         index1 = Random.Range(0, methods1.Count);
    4.     }
    5.  
    6. //and
    7.  
    8.     public void ButtonFunc0()
    9.     {
    10.         methods1[index1]();
    11.         {
    which triggers no errors. Thank you guys for the help you have given me, you guys are amazing!
     
  45. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
  46. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    I would stay away from stringbased invocation crap :) sure for RPC you need to use reflection because of serilization. But here it's much better to solve it at compile time.
     
  47. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Is what you’re saying right?

    that having a list of strings as methods known at the start of the game and calling methodlist[index] inside the invoke is somehow bad practice.

    what are you talking about you solve it at compile time using a method grabber delegate specifier but isn’t it that whatever that happens it happens to occur that a list of string is always iterated?

    Because last I checked a well made game is able to iterate a list of string without much problem.

    10th of December it’s the 23rd. Oh I don’t know anymore with this forum guys. Have a good Christmas.
     
  48. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    Why use magic strings and reflection when you can use static typing at compile time? Reflection has its place. For example when calling serlalized remote functions.
     
  49. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Using reflection can lead to troubles if you want to compile with IL2CPP later. This is a trivial problem that does not need reflection.
     
  50. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    What he's talking about is that when you start refactoring your code, this will instantly break. So yes, it's a bad practice.
     
    Max-om likes this.