[Solved] How can I change variables through different scripts?

I have a player, and a shoot prefab, The player can only be Facing in 4 directions, cause its 2D, and I have something like this in the player script:

    // Facing Direction is: 1 = Up, -1 = Down, 1 = Right, -1 = Left
    public float FacingDirectionY;
    public float FacingDirectionX;

And I have the shoot prefab script like

    public float MoveX;
    public float MoveY;
    public float MoveSpeed;

    // Use this for initialization
    void Start ()
    {
        // Example: I want to set MoveX to -1 if the player is facing Left, and only once
        //so the bullet wont curve when player changes direction

       //Something like " MoveX = PlayerScript.FaceDirectionX " (Im really new at coding)
    }

    // Update is called once per frame
    void Update ()
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(MoveX * MoveSpeed, MoveY * MoveSpeed);
        //Then MoveX will be -1, and it will move to the Left, until it gets destroyed.
    }
}

But I cant figure it out, can someone help me please?

[SOLVED :smile:]

Help you with what exactly?
Honestly, changing variables from one script to another has been answered several times on these forums. Searching around will show you several ways to do it.

Well, I have been searching for an hour and just find that A script changes B script’s variables, but I wans B script to change its own variables to the same numbers as in A script, but they are not in the same object so I cant use the same code I use to change A script’s variables.

There are lots of ways that have been posted, I know because I have answered some of them. The simple idea is you need a reference to the script. Then you need to get the variable from that script and it’s value.

//Script A

public class FirstScript : MonoBehaviour
{
public int firstValue;

void Start()
{
   x = 10;
}
}
//Script B

public class SecondScript : MonoBehaviour
{
   public FirstScript firstScript;
   public int secondValue;

public void getValue()
{
   secondValue = firstScript.firstValue;
}

Second script has a reference to first script. In this case, drag and drop in inspector, but you can also assign it some other way (using GameObject.Find for example).

While great for objects in a Scene, the above won’t work with instantiated Prefabs. Prefabs can’t have dragged values you will need to do this:

Create a tag on your player. I believe Unity projects come with a default Player Tag, or you can make a custom one:

Then in your shoot prefab:

PlayerScript  player;

void Start()
{
            player = GameObject.FindWithTag("Player").GetComponent<PlayerScript>();
              MoveX = player.FaceDirectionX;
}
1 Like

Ima try it thx

So, If the Player’s script’s name is “Movement” (Cause it was planned only to contain movement but then It became all behavior script), the player GameObject’s name is “Player”, Should it look like this?

MovementScript  player;

void Start()
{
            player = GameObject.FindWithTag("Player").GetComponent<MovementScript>();
              MoveX = player.FaceDirectionX;
}

Of course it’s not great for prefabs, this was an example of reference a script in another object and accessing it’s variables, nothing more. That’s why I mentioned GameObject.Find as another method, considering there are many ways of setting this up.

Yes, you can do it that way.

Yay

@Branthnann I wasn’t disparaging your response. It just seemed likely from the nature of the question the OP wouldn’t be aware that Prefab connections would break once they instantiated. I’ve seen that mistaken idea from a lot of people posting similiar questions.

@takatok Oh no, sorry if I sounded defensive. I understand and at least it seems OP was able to reach a solution.

I get an error message “The type or namespace name “MovementScript” could not be found. Are you missing a using device or an assembly reference?” what does that mean?

If the Play script is called Movement then the class inside says:

public class Movement {

If this is the case then you should use this:

Movement player;

player = GameObject.FindWithTag("Player").GetComponent<Movement>();

Yay it works perfectly now thank you all guys