UNITY TUTORIAL | SCRIPTING | GetComponent()

Unity Tutorial | Scripting |Getcomponent()

 

Script itself is a custom game component

also, in a script, we can modify other components such as “RigidBody”, “Collider”, or even another “Script”

to modify components in a script we need variables as a bowl for the component and we need to put the component in the variable by using “GetComponent()”

 

2
“PlayerControl” Script | Creating a Varaible
  • Create a variable “GameManager” as a bowl to bring and use “Gamemanger” script or class in this script
3
“GameManager” Script | Custom Function
  • there is a custom function, “SetGameManagerState()” in “GameManager” script or calss
4
“PlayerControl” Script | GetComponent()
  • put the class in the variable by using “GetComponent()”
  • after that, we can use the functionality of “GameManager” class in this script

 

5
“Player” GameObject | Inspector | Audio Source
  • in “Player” GameObject,
    • “PlayerControl” scrip & “Audio Source” are attached
    • we want to use  “Audio Source” in “PlayerControl” Script
6
“PlayerControl” Script | GetComponent()
  • In “PlayerControl” Script,
    • create a variable as a bowl for “Audiosource”
    • put the class by using “GetComponent()”
    • use “AudioSource”

Leave a comment