Search Unity

Call function with null reference?

Discussion in 'Scripting' started by Marscaleb, Sep 10, 2019.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    364
    I've got a function that I want passed a reference when it is called. (It's a reference to an instance of a custom script.)

    But I'm thinking I'd like an option to call this function without sending this reference. Normally to have an optional variable I just supply a default value. But since I'm using a reference, wouldn't that mean I'd have to create a new instance the use as the default value? That sounds like creating garbage. What I would want is just to be able to pass a null reference, and then my function can check for a null reference.

    But how do I do that? How can I pass a null reference to this function, or create a null reference as its default value?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,076
    Unless I'm grossly misunderstanding your question, you just supply null!

    if you have:

    Code (csharp):
    1. void MyFunction( Thingy thing)
    2. {
    3. }
    then you can call it with:

    Code (csharp):
    1. MyFunction(null);
     
    Reedex likes this.
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    364
    Really? Well that's easy.
    Thanks!
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    633
    You can also use overload methods and have one which doesn't require a reference.

    Code (CSharp):
    1. public void MyMethod()
    2. {
    3. }
    4.  
    5. public void MyMethod(MyClass myClass)
    6. {
    7. }
     
    EdGunther and Reedex like this.
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,158
    Code (CSharp):
    1. public void MyMethod(MyCustomComponent comp = null) {
    2. }
    3.  
    4. MyMethdod();
    5. MYMethod(someObject);
    6. MyMethod(null);
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    705

    While I think that above is the most elegant way to do this, for this exact case, I'd show how we get the equivalent function to avoid duplicate code.

    Code (CSharp):
    1. public void MyMethod(){
    2.    MyMethod(null); // perform equivalent call
    3. }
    4.  
    5.  
    6. public void MyMethod(MyClass myClass){
    7.   // do cool stuff here
    8. }
     
    Lysander likes this.
  7. Neriad

    Neriad

    Joined:
    Feb 12, 2016
    Posts:
    63
    As palex showed, you can also set a default value to a parameter like this :
    Code (CSharp):
    1. void Method (Parameter _param = null)
    2. {
    3.  
    4. }
    Then you can call it with or without parameter.
    Code (CSharp):
    1. void Start ()
    2. {
    3.      Parameter param = new Parameter();
    4.      Method();
    5.      Method(param);
    6. }
     
    EdGunther likes this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,281
    Pretty obvious, but you probably then want to check if the variable is or is not null before trying to use it, since you're planning to sometimes call it when it is null.
    Code (csharp):
    1. void MyMethod(MyClass myClass = null)
    2. {
    3.     if (myClass != null)
    4.     {
    5.         //do something useful with myClass
    6.     }
    7.  
    8.     //other code which doesn't depend on myClass
    9. }
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,076
    YOLO!!
     
    Joe-Censored likes this.
  10. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    4,787
    My reality is more like

    Code (csharp):
    1. void MyMethod(MyClass myClass)
    2. {
    3.     if (myClass != null)
    4.     {
    5.         //do something useful with myClass
    6.     }
    7.  
    8.     //Silently swallowing the problem and let your fellow dev pull their hair to debug and find the problem.
    9. }
     
    Joe-Censored likes this.
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,281
    Yeah but I tend to like the option of just not providing the parameter. More preference (whatever your team's coding standards) than anything else.

    Code (csharp):
    1. MyMethod();
     
  12. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    4,787
    Defualt to null is fine if it fits the use case. Your if block just reminded me how dredfull it is to debug code that silenty fails :)
     
    Joe-Censored likes this.
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,281
    Yeah it is an oversimplified example though.
     
    AndersMalmgren likes this.