Calling a function in another script

Hi,
I am sorry for my stupid first question. I am very noobish to unity-scripting.
Before Unity, I have only programmed in Flash and Javascript, which works mainly with one single file. Now, here are so many files, which confuses me a bit.

I simply have the question, how to call a function from an other class.
Meaning:
I have one script, which contains a function, that I want to call from different scripts. Lets say, this is the class with the function I would like to call:

using UnityEngine;
using System.Collections;

public class patsch : MonoBehaviour {

	public void laerm(){
		print("Lärm");
	}
}

And I have an other script like this (attached to a rigidbody):

using UnityEngine; using
System.Collections;

public class faden : MonoBehaviour {

void OnMouseDown() {
float rnd = Random.Range(-50,50);
rigidbody.AddForce(0,0,100);
rigidbody.AddTorque(0f,rnd,0f);

      // I would love to call my function on this place
} }

How do I do this?

Regards
Chricken

In order to call a method from one class to another class, the way may depend on the purpose and use.

So first off, do not fall in the trap of “Make it static”.

If your method acts on the object then no static, if the method work more as general feature of a class then yes you could go for static.

Take the Vector3 class,

Vector3 vec= new Vector3(10,10,10);
vec.Normalize();

is a method that modify the actual vector object, so this one should be a instance method.

Vector3 vecA = new Vector3(10,10,0);
Vector3 vecB = new Vector3(20,20,0);
float dot = Vector3.Dot(vecA,vecB);

in this case vecA and vecB are used but not modified so a static method is defined. Actually, you could make dot an instance method that you used:

Vector3 vecA = new Vector3(10,10,0);
Vector3 vecB = new Vector3(20,20,0);
float dot = vecA.Dot(vecB);

but it seems less intuitive.

Also, static methods do not allow to use instance members so if your method should use any member of the object, bam, you cannot or you have to pass them as argument…

Now considering you have this:

public class ClassA:MonoBehaviour{
   int a;
   public void PrintA(){
       print(a);
   }
}

See, a is an instance member so if PrintA was static, it could not see a.

Now other class:

public class ClassB:MonoBehaviour{
    public void DoSmgWithClassAMethod()
    {
        GetComponent<ClassA>().PrintA();
    }
}

And GetComponent is how you access members from other classes. There are loads of examples so I won’t go any further into this since you can find a lot of them using google.

For Calling a function of one class from another you should make that function as static. And you can call that static methods from the another classes. If your method contains non static variables, you can create a static Instance.

Class Patsch:

using UnityEngine; 
using System.Collections;

public class patsch : MonoBehaviour 
{
   public static patsch Instance;

   void Start ()
   {
     Instance=this;
   }

   public void laerm()  //<---- public method
   {
        print("Lärm");
   }
}

And the another class is

using UnityEngine; 
using System.Collections;

public class faden : MonoBehaviour 
{
        void OnMouseDown() 
        {
           float rnd = Random.Range(-50,50);
           rigidbody.AddForce(0,0,100);
           rigidbody.AddTorque(0f,rnd,0f);
         
           //Call that function
           patsch.Instance.laerm();   //<------ Call method
        }
}

Also go through the naming convensions for c#

If you have a method in ClassA thus:


public Class ClassA : MonoBehaviour

void PrintName()
{
Debug.Log(“YourNameHere”)
}


In order to activate PrintName() from ClassB, you can simply have this:

public Class ClassB : MonoBehaviour

public GameObject ClassAObject;

void Start()
{
ClassAObject.SendMessage (“PrintName”);
}