- Home /
How do I use GetComponent in one Line
Hi I am trying to find a way to use GetComponent without the use of an extra variable in one line. Here is what I have so far. GameObject.Find("object").GetComponent("script").var=0;
Answer by kacyesp · Aug 25, 2014 at 05:32 AM
I'm assuming you've already attached a script to some game object. Assuming you want to get, say a BoxCollider2D that is attached to the same game object, you could use:
BoxCollider2D collider = GetComponent<BoxCollider2D>();
Your answer
![](https://koobas.hobune.stream/wayback/20220613161429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Best way to access variables of script from granchild of another object, into a different script? 1 Answer
Find GameObjects with a true boolean and put them in an array? 1 Answer
Need help with in game custom clock! 0 Answers
why wont this SendMessage work 1 Answer
How to get the value of a boolean in game object to another Game object script. 1 Answer