Project
Beginner Gameplay Scripting
GetComponent
Tutorial
Beginner
5 Mins
Unity Technologies
Overview
You have completed 0% of this Tutorial.
1.
GetComponent
Summary
How to use the GetComponent function to address properties of other scripts or components.
Language
English
Recommended Unity Versions
4
1.
GetComponent

UsingOtherComponents

using UnityEngine; using System.Collections; public class UsingOtherComponents : MonoBehaviour { public GameObject otherGameObject; private AnotherScript anotherScript; private YetAnotherScript yetAnotherScript; private BoxCollider boxCol; void Awake () { anotherScript = GetComponent<AnotherScript>(); yetAnotherScript = otherGameObject.GetComponent<YetAnotherScript>(); boxCol = otherGameObject.GetComponent<BoxCollider>(); } void Start () { boxCol.size = new Vector3(3,3,3); Debug.Log("The player's score is " + anotherScript.playerScore); Debug.Log("The player has died " + yetAnotherScript.numberOfPlayerDeaths + " times"); } }

AnotherScript

using UnityEngine; using System.Collections; public class AnotherScript : MonoBehaviour { public int playerScore = 9001; }

YetAnotherScript

using UnityEngine; using System.Collections; public class YetAnotherScript : MonoBehaviour { public int numberOfPlayerDeaths = 3; }

Woohoo! You nailed this tutorial.
Continue rocking your Beginner Gameplay Scripting project.
Next Step:
Delta Time
Tutorial
Beginner
5 Mins
Unity Technologies