How do you acess variables from different scripts?

I’m working on a FPS project with UnityScript and I am trying to make it so that you can’t move the player when you’re riding a vehicle. I have a Boolean on the player script, canMove. I need to make it so that if canMove is false, then the vehicle should move. How do I access the canMove Boolean (which is in the player script) from the vehicle script? Sorry about such a basic question, but I’m new to this.

Try to look around for GetComponent<>();
You’ll get plenty of answers as this is a common question :slight_smile:

Assuming that you know how to declare variables add static before var. Let’s name a script "A"

Then from script B type . (E.g: A.hi)

After that you’ll be able to access and change your variable.

If you don’t understand then search the internet, you’ll find plenty of results.

Don’t do that; using static means there can only be one instance of that variable. Sometimes this is what you want, but a lot of the time it’s not. Use GetComponent, which always works.

–Eric

1 Like

So I can use GetComponent() to access a non-static variable?

Of course you can. Providing it has public access.

1 Like

You could try this:

public PlayerScript playerObject;

void Update ()
{
    if (playerObject.canMove)
    {
        //Do Whatever
    }
}

This is in C#, this might be UnityScript but I have never really used UnityScript so I may be wrong, someone correct me if I am. But before you continue, make sure, if you get this to work, that you grab the GameObject that has the “PlayerScript” attached to it, onto the slot in the Inspector with THIS script on it. Anyway, here is the UnityScript(I think):

var playerObject : PlayerScript;

function Update ()
{
    if (playerObject.canMove)
    {
        //Do Whatever    
    }
}

Ya you can reference the script directly like that or make a varialbl of your script type and use getComponent () in your start method to store a reference to it