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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I call a method from one class from within a separate class

Discussion in 'Scripting' started by Phanixis, Dec 4, 2015.

  1. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Title says it all. Basically I have a C sharp script with

    ClassA
    {

    public void Method1 ()
    {
    stuff
    }

    }

    ClassB

    {
    void Method2 ()
    {
    ClassA.Method1 ();
    }

    }

    This results in a "An object reference is required for the non-static field, method, or property 'ClassA.Method1 ()' " error message.

    I obviously need to create an instance of ClassA within the ClassB script before running this method, but I cannot seem to figure out how.

    Thanks for the help.
     
  2. Kamilche_

    Kamilche_

    Joined:
    Jan 11, 2015
    Posts:
    75
    Replace the body of Method2 with:
    {
    ClassA obj = new ClassA();
    obj.Method1();
    }
     
  3. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Thanks for the help.

    Unfortunately, now I am getting a warning within Unity itself:

    You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all.

    Oddly enough, the code still appears to be working.

    One follow up question though. Say I want to create want to insure Method1 is called from the same instance of ClassA using different instances of ClassB, is there a way to do that?
     
  4. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    So put each class in a different script and "add component" to your GameObject B. Like this

    Code (csharp):
    1.  
    2. GameObjB :MonoBehaviour
    3. {
    4.        ClassA _instance = GetComponent<ClassA>();
    5.         _instance.Method1();
    6. }
    7.  
    Or use a public property in class b that you drag from your hierarchy or prefabs ClassA into the inspector for class bs property. Then

    Code (csharp):
    1.  
    2. public ClassA _objClassA;
    3.  
    4. Start()
    5. {
    6.       ClassA instance = Instantiate(_objClassA, new Vector3( 0, 0, 0), Quaternion.identity);
    7.       instance.Method1();
    8. }
    9.  
    Sorry for typos I am on iPad. Crappy typing.
     
    Last edited: Dec 4, 2015
  5. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    I tried your first method but I am getting the error message:

    A field initializer cannot reference the non-static field, method, or property 'Component.GetComponent<ClassA>()'

    Alternatively, I can just used the command

    ClassA instance;

    to declare the variable and then within ClassB.start I can write

    instance = GetComponent<ClassA>();

    which the compiler accepts. Unfortunately, and attempt to call ClassA.Method1 from ClassB when running the code gives the following error

    NullReferenceException: Object reference not set to an instance of an object.

    Perhaps I need to create a single instance of MethodA elsewhere before calling this script?

    Thanks for the help.
     
  6. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Just because the compiler accepts doesn't mean it is correct. NullReference means you didn't instantiate your object. GetComponent works by getting a script or other component that is attached to a game object in the hierarchy by selecting "Add Component" on your game object in the U.I. scene mode inspector, adding ClassA script to the object.

    Try just ClassA _clsA = GetComponent<ClassA>(); after you add component.
     

    Attached Files: