How i call function or method from another script in c# ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ThaksinCCC

Let’s say i have Player scene with a Player.c# script

In player.c#

public void gameStart()
{
    Gd.Print("gamestart");
} 

How i call gameStart() in main scene with main.c#

main.c#

public override void _Ready()
    {
        //Call gameStart() from Player.c#
    } 

I only know at GDscript

func _ready():
 $Player.gameStart()
    
:bust_in_silhouette: Reply From: davidoc

You need to get a reference to the node and cast it to the proper type:

var player = GetNode<Player>("Player");
player.gameStart();

I already tried this before it not work but now i can fix this problem .

ThaksinCCC | 2021-07-21 05:26

:bust_in_silhouette: Reply From: ThaksinCCC

Now i can fix this problem

var player = GetNode<KinematicBody2d>("Player");
player.Call("gameStart");