- Home /
Question by
princeadj14 · Nov 30, 2013 at 05:08 PM ·
variablegetcomponentpublic variablegetcomponentinchildren
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
Comment
Best Answer
Answer by robertbu · Nov 30, 2013 at 05:45 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Passing a Script Name to a Function 2 Answers
How to set a variable equal to another variable in another gameobject? 2 Answers
Why can't I change another script's variable with this script? Thanks, 1 Answer
Making a variable work in two separate objects? 4 Answers
AddComponent with parameter variable 2 Answers