- Home /
Accessing other scripts using GetComponent()
I've been working on my runner game and I'm having some difficulty making using of GetComponent. What I want to do is have a script: GameController store an enemy speed variable that every second, I want it raise the global speed of my enemy objects.
My problem is that I'm having trouble figuring out how to use GetComponent to get it to work. Is there a way to access the script: EnemyBehavior directly and change the enemySpeed variable in it? Or do I need to do something else to modify the behavior?
What I did that didn't work was the following:
public var lSpawnLoc: GameObject;
public var fSpawnLoc: GameObject;
public var startSpeed: float = 0.8;
private var spawnObject1 : SpawnObject;
private var spawnObject2 : SpawnObject;
private var enemyBehavior : EnemyBehavior;
private var newEnemySpeed : float;
function Start ()
{
spawnObject1 = lSpawnLoc.GetComponent(SpawnObject);
spawnObject2 = fSpawnLoc.GetComponent(SpawnObject);
InvokeRepeating("Spawn",3.0,3.0);
enemyBehavior.GetComponent(EnemyBehavior);
InvokeRepeating ("AddSpeed",1.0,1.0);
}
function Spawn()
{
var choice: int = Random.Range(0,2);
if (choice == 0)
{
spawnObject1.CreateEnemy();
}
else
{
spawnObject2.CreateEnemy();
}
}
function AddSpeed()
{
enemyBehavior.enemySpeed = newEnemySpeed;
newEnemySpeed += .01;
}
On my phone so can't reformat your code (highlight it and press the code 101 button). Check out the GetComponent tutorials on http://unitygems.com
The Unity Learning $$anonymous$$odules also have tutorials on beginner Unity scripting, including a GetComponent tutorial.
I reformatted the code.
I have another question about getcomponent, when using it on a gameobject, do I need to have a public variable that is assigned the other game object or script that I want to access or not?
The variables, classes and functions you want to access from another script needs to be public.
Converted your answer to comment. Please only post answers if they are answers.
If you feel this question is answered, tick the box next to the answer you think is the best and close this question.
Also have a look at the FAQ http://answers.unity3d.com/page/faq.html
Answer by CVogel · Aug 17, 2013 at 09:50 AM
Let's say you have a GameController Script with an 'enemySpeedIncrease' variable defined.
GameController -> enemySpeedIncrease.
Now, from other scripts in the same scene, you can access this GameController Script as follows. Let's say you have an Enemy object with an EnemyScript script. In EnemyScript...
EnemyScript (C# example)
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour {
public float currentSpeed;
public GameController GMC;
// Use this for initialization
void Start () {
GMC = FindObjectOfType(typeof(GameController)) as GameController;
}
// Update is called once per frame
void Update () {
currentSpeed += GMC.enemySpeedIncrease;
Debug.Log ("Enemy Speed is: " + currentSpeed);
}
}
You see you have to give your 2nd script a reference to the first script, and load it with FindObjectOfType or a similar function.
Hopefully this helps!
Chris Vogel
Using FindObjectOfType is not appropriate. What if you have two of the type, the method will return the first found. What if you were looking for the other?
The appropriate way is GetComponent.
So on the EnemyBehavior script, I would set a variable to type GameController . And say something along the lines of enemySpeed= variable.GetComponent(newEnemySpeed) ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613112400im_/https://answers.unity.com/themes/thub/images/avi.jpg)