How To Access Variables From Another Script In Unity



This video will show you how to access variables from another script in Unity.

Download Project Search & Replace https://assetstore.unity.com/packages/tools/utilities/project-search-65234?aid=1100l3e8M&pubref=ad3

💬 Join Our Community

– Discord: https://discord.gg/NjjQ3BU
– Patreon: https://www.patreon.com/infalliblecode
– Newsletter: http://infalliblecode.com/newsletter/

❤️ My Favorite Unity Assets *

– Odin Inspector and Serializer: https://assetstore.unity.com/packages/tools/utilities/odin-inspector-and-serializer-89041?aid=1100l3e8M
– Editor Console Pro: https://assetstore.unity.com/packages/tools/utilities/rainbow-folders-2-143526?aid=1100l3e8M
– Rainbow Folders 2: https://assetstore.unity.com/packages/tools/utilities/rainbow-folders-2-143526?aid=1100l3e8M
– Peek: https://assetstore.unity.com/packages/tools/utilities/peek-149410?aid=1100l3e8M
– Project Search & Replace: https://assetstore.unity.com/packages/tools/utilities/project-search-replace-55680?aid=1100l3e8M

⚡ Unity 3D Plus: http://prf.hn/click/camref:1100l3e8M/destination:https://store.unity.com/products/unity-plus
⚡ Unity 3D Pro: http://prf.hn/click/camref:1100l3e8M/destination:https://store.unity.com/products/unity-pro
📧 Follow and Subscribe

– Twitter: https://twitter.com/infalliblecode
– YouTube: https://www.youtube.com/infalliblecode?sub_confirmation=1
– Website: http://infalliblecode.com

👋 Contact me directly at [email protected]

* Disclosure: These are affiliate links, which means I’ll receive a commission if you use them to make a purchase.

source

25 thoughts on “How To Access Variables From Another Script In Unity

  1. Collaborators are hell. On one side making a single script handle everything is just wrong and gets messy.
    On the other hand I want to avoid dependencies as much as possible. Wish I remembered that one video on Scriptable Objects about how to avoid this with them.
    So that deleting or removing a script or player doesn't cause the whole thing to collapse and go error.

  2. I've had this as my nightmare for so long.
    Every single time I want to work on my player control script i run into this issue: How do I get a value to other classes to control or change it?!
    Say I have different states and each affects the movement in some way. I want the main movement to be alone and let others manipulate it. To have a system so that anyone can change and push it, without having to write into them selves updates on everything else that needs to be updated.
    Imagine writing a moving state, idle state and running state and every single one has to update sliding value, falling value, or any other value associated with the main movement.

    Or maybe you have drag and friction and want to change it when you're on ice. But what if you want more freedom. Want others or yourself to be able to automatically just create a new surface.
    Would you want to have to code in every single time the default values when the player don't touch anymore or leave that area? Having to re code every scenario so that the player doesn't get stuck with 0 friction everywhere and having to update that state every time just to check whether the player is no longer affected?

    This is an issue some games have had and been exploited by speedrunners such as Rayman 2. Where the water only changes when you enter and leave the state, so they found ways to get out of the water without it registering and go back to regular movement.
    These things haunt me as a perfectionist and someone who wants to give as much freedom as possible to both the player and the creator. See it as: What would I want if I was a modder? Making it all accessible and possible to add new things without messing it all up.

  3. good explanations of the way these things work but your title is misleading. Please change the title to something more technical to more accurately reflect the level of content.

  4. I'm confused.
    I'm a new Unity Developer and I'm wondering in which cases a Collaborator wouldn't serialize their dependencies in the inspector.

    The only reason I can brainstorm up is in the case of something like network programming or user information management where security is vital.

    Why would a script not announce what it's depending on?

  5. Not sure why this video has so many dislikes when it answered what exactly I am looking for (I got here after the Singleton video). Your style of explanation is superb. Keep 'em coming!

  6. unity is such a fuck ass for making this so stupidly difficult. a variable is a fucking variable. just fucking find it already, jesus fucking christ.

  7. Heres the solution for people who were disappointed by this video:
    Example:
    =====================================================================================================================
    public Class ScriptA{
    public string myStringVariable;

    }
    =====================================================================================================================
    //You want to access myStringVariable from ScriptB
    public Class ScriptB{

    //Get reference to scriptA first

    public ScriptA scripta;
    //Then use the created reference to call the variable. Note: make sure you save all the code first otherwise it may not pop up when you press scripta.
    scripta.myStringVariable;
    }
    =====================================================================================================================
    Thats basically it. Make sure its public or if you want private or protected then you will have to use a different method which is coding a getter and a setter, another term used is accessor.
    Example 2 – For private variables:
    =====================================================================================================================
    public Class ScriptA{

    private string myStringVariable
    ;

    //Note: its spelled differently to the variable
    public string MyStringVariable

    {

    //The get returns the private variable;
    get { return myStringVariable; }

    // This is the setter
    set { MyStringVariable
    = myStringVariable
    ; }

    }

    }
    =====================================================================================================================
    public Class ScriptB{

    //Get reference to scriptA first


    public ScriptA scripta;

    //Create new string variable for this class so we can use it get the myStringVariable
    data from ScriptA's class and assign that data to our variable in
    //this class
    public string copyStringFromScriptA;
    //this calls the setter

    npcm.MyStringVariable
    = "change string";

    //this calls the getter – copy the ScriptA's variable data & stores into our variable copyStringFromScriptA

    copyStringFromScriptA = npcm.Variable;
    }
    =====================================================================================================================

    ENJOY

  8. So if code is running in an OnTriggerEnter() event and I know the collider is another Player (because I've checked the tag of the collider's object to make sure it's a Player), how can I change a public variable in one of that player's scripts (E.g. set a bool called isTagged to true)?

    Visual Studio/the code doesn't know at the time of compiling that it's going to be a Player that collides with the collider, so it doesn't give me the option to reference isTagged or even the script that isTagged is in. Any ideas?

  9. I came here with what I thought (hoped) was a simple question and just got confused. Your use of specific terminology and jargon over layman's terms makes this impossible to follow unless you already know enough about the subject matter to not need this guidance in the first place.

  10. One suggestion for Charles. This video is meant for people with a little bit more experience. It's technically a "best practice" video. Having this in mind, parts where you explain what a script is, are absolutely unnecessary as people who would be interested in this topic surely already know that much. It made me lose interest very quickly and I can imagine there were more who felt the same way. Try not no mix beginner things with more advanced things in the same video.

Leave a Reply

Your email address will not be published. Required fields are marked *