Search Unity

Use in the method the instance of the object that called it

Discussion in 'Scripting' started by MikeyJY, Nov 19, 2021.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I have this situation:

    Code (CSharp):
    1. class newClass{
    2.    public newClass(){
    3.        //constructor
    4.    }
    5.    public float newMethod(newClass new){
    6.         return afloat;
    7.    }
    8.    public newClass oldMethod(newClass new1, newClass new2){
    9.        //Now I need to use the newMethod for the parameters new1 and new2, however would be nice to call:
    10.        new1.newMethod();
    11.        //and not:
    12.        newMethod(new1);
    13.        return just_newClass;
    14.    }
    15. }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,999
    I don't quite understand your question. Your method
    Code (CSharp):
    1. public float newMethod(newClass new){
    2.     return afloat;
    3. }
    Has a "newClass" parameter that is not used at all in that method. So why do you have declared that parameter in the first place? Your method is already an instance method. So you have to use:

    Code (CSharp):
    1. float val = new1.newMethod();
    in order to get the value from that instance. Almost nothing in your sample code makes any sense. What's "afloat"? What's "just_newClass"? And overall your naming of the class, method and parameters is horribly non-descriptive.