Call a funktion from another class when coliding

Hey, i have use the last hour crying over this simple task, and to be fair i have no clue why its not working :frowning:

Im having a network game, and when a player is hit i want them to be knocked back (Pretty simple) but everything is giving me a hedack right now. I have 2 questions i hope someone can answer:

Call A Funktion in another class when coliding:

Curently i have the 2 scripts seen here:


public PlaerControl Player;

void OnCollisionEnter(Collision other)
   if( == "Player")
      PlaerControl = PlaerControl.GetComponent<PlaerControl>();


public void Kncokback(float Distance)
      Vector3 knockbackDir = new Vector3(0,0,-1);
      transform.Translate(Distance * knockbackDir * Time.deltaTime);

I cant get it to work that its calling the Knockback funktion when coliding.

The 2 Errors i get is:
Assets/Scripts/Spells/FireballScript.cs(39,55): error CS0120: An object reference is required to access non-static member UnityEngine.Component.GetComponent(System.Type)'* *Assets/Scripts/Spells/FireballScript.cs(40,39): error CS0120: An object reference is required to access non-static member PlayerControl.Knockback(float)’

Beside that my other question, how do i calculate the angel of the fireballs hit, so i can make the knockback be in the diferent direktion (As kncokbacks should work), insted of just knocking back the player in the direktion he is not facing.

Thanks for your time to read this, hope someone can help :slight_smile:


I think I know what’s wrong here.

In your FireBall Script, under your collision function you have written:

PlaerControl = PlaerControl.GetComponent<PlaerControl>();

This should be:

Player = PlaerControl.GetComponent<PlaerControl>();

You need to assign the retuen value of GetComponent to a variable! :slight_smile:

PlaerControl whateverYouLiek = other.collider.gameObject.GetComponent<PlaerControl>();


So in the line where you call GetComponent you are assigning the return value to a “variable” with the same name as the class. You can’t do that!
You can’t reuse the same names for different things in the same context (global). I suggest you instead give that variable a different name, preferably just without a capitol and the rest should work :slight_smile:

Hope this helps,