- Home /
Accessing a variable from another script on the same object? C#
Hello again. For my game I am trying to get my clickToMove script to access my targeting script so instead of when you right click an enemy and it targets and then continues to walk to the location that you click, instead it takes the target from the other script and walks in front of the target instead. The problem is I don't know how to access the variable on the other script.
The game component is a sprite.
I have checked quite a few different answers but none of them seem to work or apply to what I'm trying to do.
Here is the target select bit:
private Transform selectedTarget = null;
void Update(){
if (Input.GetMouseButtonDown(1)){ // when button clicked...
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
//Debug.Log (pos);
RaycastHit2D hit; // cast a ray from mouse pointer:
hit = Physics2D.Raycast (pos, Vector3.zero);
if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
DeselectTarget(); // deselect previous target (if any)...
selectedTarget = hit.transform; // set the new one...
SelectTarget(); // and select it
}
}
}
There may be more than one ways to do this, if I were you this is how I will do it 1. Have a gameobject parameter for SelectTarget method 2. Pass gameobject hit 3. Use GetComponent to get target's script and it's variable.
Like this
if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
DeselectTarget(); // deselect previous target (if any)...
selectedTarget = hit.transform; // set the new one...
SelectTarget( hit.transform.gameObject); // and select it
}
void SelectTarget( GameObject target )
{
variableType variableFromTarget = target.GetComponent<targetScript>().theVariable;
.
.
.
}
Answer by Owen-Reynolds · Jan 16, 2014 at 08:01 PM
You look at one of your other scripts the same way you look at a script on someone else, using getComponent.
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html is the official reference man page. Sundar (comment above) has an example in SelectTarget.
Since the place to look is "you," you can just write GetComponent with nothing in front of it. Or transform.Get...
or gameObject.Get...
if you prefer the "never leave it blank" style.
Ok so basically I need to bull the transforms position into the movement script. So would I add the position variable into the SelectTarget Void and the type this,
OtherScript otherScript = GetComponent<OtherScript>();
otherScript.DoSomething();
Into the update part of my movement script so i can access it? Something like:
TargetScript targeting = GetComponent<targeting>();
targeting.SelectTarget();
?
Sorry was being stupid, I've got it now, thank you :)
Targeting targeting = GetComponent<targeting>();
targeting.SelectTarget();
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Character doesn't stop and sits on top of target. 1 Answer
Multiple Cars not working 1 Answer
Trigger player movement on touch 1 Answer
Distribute terrain in zones 3 Answers