Question by
Sherlono · Jan 29, 2016 at 11:07 PM ·
scripting beginnervariablespositioning
Call the position x of a 2D object from another script.
So I have 2 "characters", each has its own script and an object with a script that should store the difference between this characters' position in x in a float variable for later use but i can't find the way to use this characters information in this script. I am using C#. The following is just to give an idea of the final product: Once I have this value i can compare it to 0 and use it to make the characters to always look at each other (yes, like in a fighting game).
Comment
Best Answer
Answer by Sherlono · Jan 30, 2016 at 02:35 AM
Solved it:
public float PlayerOnexPos;
public float PlayerTwoxPos;
public bool facingRight;
void Update () {
PlayerOnexPos = GameObject.Find("Player").transform.position.x;
PlayerTwoxPos = GameObject.Find("Enemy").transform.position.x;
if (PlayerOnexPos < PlayerTwoxPos)
{
facingRight = true;
}
else
{
facingRight = false;
}
Your answer
Follow this Question
Related Questions
Enemy Variable Battle System 1 Answer
Changing a variable in another script on Raycast hit - C# 1 Answer
Unable to set String Variable in Script 0 Answers
Question on setting a variable to click 0 Answers