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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Using of script on methods

Discussion in 'Scripting' started by Sylon87, Dec 16, 2018.

  1. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    hello everyone.

    i hope that somebody can help me to understand this...
    i saw sometimes in some code peapole that do something like this

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.         DoSomething(this);
    16.        
    17.     }
    18.  
    19.     private void DoSomething(GameManager game_manager)
    20.     {
    21.         if (1 >2)
    22.         {
    23.             Debug.Log("done");
    24.         }
    25.     }
    26. }
    27.  
    the point is... they won't use game_manager on the body of the method, but they declare it... there is something behiund it?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Depends on what "DoSomething" is. There could be a lot of explanations, most of which involve a potential function that DOES use the parameter.

    1) If DoSomething is a method called by Unity (like OnCollisionEnter, to name one) and that method is called with a parameter, then Unity won't recognize the method when it goes to call it.
    2) if the method comes from an interface, then the same thing as #1 applies. (If this is the case, there'd be interface(s) specified after the class name and inheritance at the top of the file)
    3) If the method looks just like it does here but isn't one of Unity's called methods, then it's almost certainly there because the method used to use the parameter, but doesn't now, and the dev either was too lazy to remove the parameter from everywhere the method is used, or chose not to to preserve backwards compatibility with any code that might use it (though if it's private, that's unlikely).
    4) The dev planned to use the parameter and never got around to it, and never removed it for the same basic reasons as in #3 (laziness, or future-proofing).
    5) If DoSomething overrode a method in a parent class, it'd need the same parameters.

    I think #3 is most likely, but with an obfuscated, "in your own words" example, it's hard to be sure, especially if there was something in the original code you inadvertently omitted.
     
  3. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196

    hello!
    thank's you for your reply, actually i just forget something my mistake, what i see is that on that code they are using it on a callback system, so they pass as argument the base class... but i didn't got it...