How to get Component from Game Object in C#

//This script is in C#

public GameObject PlayerScript;

PlayerScript= (CollectPapers)gameObject.GetComponent();

//noted that CollectPapers class is made in javascript

1 Answer

1

I’m assuming ‘PlayerScript’ is a script/class. I’m assuming it is on the same game object as the script above.

public PlayerScript playerScript;

void Start() {
   playerScript = GetComponent<PlayerScript>();
}

I’ve made ‘playerScript’ public bacause you did in your code above, but typically it would be private.