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
NEXT VIDEO: Design Patterns in Game Development
👉 https://youtu.be/qS2SPl3GE3k
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.
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.
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.
As a beginner there's a lot of terms you use… now am confused also this are different ways to access not "how to"
Jesus… three minutes in and just a bunch of hot air blowing!
is there a way to set a bool as inactive or active from another script it was not very clear
Yeah not helpful at all, for anyone new.
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?
You look like my gta character lol
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!
Wtf are u talking about! Dislike!
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.
This video is unnecessarily long
"how to" is a bit misleading for people like myself looking for a tutorial. This video amounts to a list of ways you could do it.
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
fuker. this is shit
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?
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.
gonna be honest this didnt help at all
Why are you flexing your pc
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.
you talk too much. this should be a 3 mins video and you made it 8.
Am I the only one in the world that struggles with saving High Score on another scene?????
You can use generic unity events to pass vairables when needed.