Help creating function for changing vars in another script

Like all inter-script interaction, all you need is:

  • Script A needs a reference to script B, which you will populate in the editor:
// inside of ScriptA:
public ScriptB myBScript;
  • Script B needs to have something public that script A can change:
// variable / field inside of ScriptB:
public int myFooInteger;

// function inside of ScriptB:
public void DoStuff( string reason)
{
}
  • Script A can then change it directly using the reference to B:
// here is ScriptA accessing something in ScriptB
myBScript.myFooInteger = 1;
myBScript.DoStuff( "Please");

This is basically The Reference Unity Pattern™ for inter-script reference. Live it, learn it, love it… tons of code operates with this fundamental pattern.

6 Likes
Pls Help Me Reference a Function from Another Script
how do i change a bool variable from true to false from a different script than the variable is in?
Why isn't my variable carried over?
how to make my enemy attack the player?
Navigation UI System with Camera move in 3D with Panels
(SOLVED) Data Access from another script
How can I get one Boolean to be accessed by another script?
Get a float from another Object/script
IF/THEN Ranged Statements + Other script Interaction
Acceed variables from other scripts
Get Vector3 way points from another script.
Update ammo with {get; set}
Pass a variable from one object to another.
Click on an object and have it trigger code on another game object...
Calling script using inside another script
Get variable from another script
How can I access and apply an AudioSource from 2 different objects?
NullReferenceExp. I really Need Help!
Object Reference not set to an instance of an object (926972)
Need help getting a 3d object to destroy when hit by a 2d bullet in a 2d game
Not sure how to call a public void from another script
How to acces and change the float that is in diffrent script?
C# Health and Damage Script help needed
Anyone have tips on making an item shake after a wrong guess?
keyword public not working
get and set property from other game object script....
How to properly call a variable in a separate script
Uses same variable in different scripts
How to link one script function to another script
How do you transfer variable values between scripts
How to check bools in a different scirpt
Reading variables from another script?
How Do I Write a Script to Change the Audio Clip on an Audio Source?
how to change make my movement (rigidbody) relative to my camera? (cinemachine)
How To Get A Field,Type,Value of a variable on another script
Help referencing a script from another game object
Rotate an object from another script
trying to run the class from one script in another script I get an error Please Help me?
Unable to find a good way to reference a GameObject from another script
Help, how do i call variables from another script
how do i reference another gameobject's variables correctly?
Cannot access the variables/parameters from another script
Check if a script of another object is on? (C#)
How do I make it so that when my snake picks up a food it gets +1 point added?
Help im a new coder and i dont know how to tie variables
how do i made a script interact with another?
Access/enable/disable bool from another script
How to change/set animator controller from other script?
Is this a proper way for one script to reference another?
GetType.GetField.GetValue from another script as GameObject
How do I change vaule of one script using another?
Passing an object as an argument
Help needed accessing a variable from another script
man I really dont understand "talking" between scripts
Which function to acces another scripts
how to refrence object
Need help with variable referencing in another script
Which Function I Need to Use to Receive Figures from Another Script?
I am having trouble getting a reference to another class
Cannot Set a variable of a class within another clas
Can't access one class from another
How do I link scripts?
Referencing a Method in another Script - Beginner
Public variables (839202)
How to sent the transform value of a Gameobject to another script?
accessing bool from another class not working
How to reference a variable from another script without using static variables
Trouble disabling a script from another script
Referencing the child of a prefab
Can't get variable from another script
How to you see if a bool is true for false on a different object
Trying to access childrens variables from parent (925582)
Transform.position to an emty game object
Trying to access a function in one script from another
Calling Rigidbody from another script and appying AddForce to it.
call function in clone
How to connect two different scrip?
I need help accessing my float between 2 scripts
How can I pass on the variable from another script to another?
How do I make it possible for two different files to use the same dictionary
How can I call a function from a nearby gameobject's script?
Best way to get variables/components from other scripts
Stop moving whilst attacking
Is there a way to get a value of a variable from a script to onother?
How do I access the Animator component in this case?
How to get a bool from another class that is get/set
bool only equling at true in one script but not the other
Need help refracing script inside text
Hi! how do i access a randomly generated string in one script from another script?
Problem accessing variables inside an object array
How do I automate getting the getting GameObject into a script?
NullReference exception() even when i have everything linked in the hierarchy
How to make a reference to a script?
Getting instance of an object from another objects script
How to create a homing missile in reference to another script
Destroying Clones Simple Issue
Value not subtracting and I don't know why
Trying to pass variables from one script to another
Calling a Scriptable Object using a String
How to reference a public variable from a script in one scene, in a script from another scene
refrencing a script within a script
How to access a Game Object from another script in Unity?
Need help acessing variable from different script.
Check if the Camera turns to the left or to the right from another Script
Is it possible to change the parent of an instantiated prefab from another script?
Referencing instantiated scripts
Cant figure out how to reference different class without vs code crying
How can you get a bool value from one script to another?
Getting my scripts to work together
How to get an int from another script?
Move object instance via another script
Referencing and setting an enum state in one script from a different script
Script A doesn't recognize a Variable I get from Script B, tried many solution in this forum, but still failed
How to use other objects instead of gameObject in onCollisionEnter()?