-2

I am confused about how to access game objects or components in unity with GetComponent<>.

If for example I wanted to access the position of a game object named player that also contains a script called player how would I use getComponent for that? Or is there another way of doing it?

Also how would I access variables inside of a script with getComponent? I'm just confused as how to use it because sometimes it will work and other times it will throw errors.

Also when accessing a game object, when do I capitalize or not capitalize GameObject?

As you can see I am very unsure as to how this works and I am new to unity, so any answers or help would be appreciated.

Thanks in advance.

  • 3
    Please consider a tutorial or Unity's [documentation](https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html). This site is for debugging and not for step-by-step guides on well documented concepts. – Erik Overflow Jan 23 '20 at 22:28
  • There are a lot of questions up there. You have to look at things. Findobject... works on things in the hierarchy components are the features of an object. The bits you can access by get component are public things. If this is all a big struggle there’s plenty of tutorials and books and so on out there. Unity do a great job as do a number of you tubers, people in skillshare, udemy etc – BugFinder Jan 23 '20 at 22:38
  • Does this answer your question? [How does Unity's GetComponent() work?](https://stackoverflow.com/questions/44105058/how-does-unitys-getcomponent-work) – devNull Jan 23 '20 at 23:07
  • BugFinder, thank you, are there any youtubers in particular you suggest checking out? –  Jan 24 '20 at 02:07
  • Erik Overflow, thanks, I have checked out their page on it but I wasn't sure if they meant to use it for components of game objects or actual game objects. –  Jan 24 '20 at 02:11
  • @goatman1 I think you should also get into some general c# beginner tutorials ... You use `GameObject` when referring to the type `GameObject` for either declaring e.g. a field or access one of its `static` methods. You use `gameObject` which is a "build-in" property of all `Component`s that holds a reference to the according `GameObject` that `Component` is attached to .. I didn't understand your last comment .. it is called `GetComponent` not e.g. `GetGameObjectThatHasComponentAttached` ;) – derHugo Jan 24 '20 at 06:10