How do you communicate between scripts?

Okay, I have a javascript in one object, and another script in another object.

I want them to talk.

How do you:

  • Change a public variable in another script/object?
  • Access a function in another object, or even the same function in another script file.
  • Cause events based on communication in another script. Pretty much the above item.

Generally, I need scripts and objects to talk to each other, and have NO idea how it would be done. If any answers could be offered in javascript, I would be appreciative.

Thank you,
Foolish Frost

The name of the script is a type, so you could put a variable into your function for the other script like this: var script1 : ScriptName;

In order to access variable and functions inside, do this: script1.variableName = whatever; or script1.functionName(passedVariables);

Ah, but how do we talk to scripts in OTHER objects?

And yes, this is working very nicly for single object communications.

You declare a variable in one object, using the script name in the other object as its type:

var oScript: OtherObjectScript;

Then, select the first object in the hierarchy, and in its inspector, you should see a variable for OtherObjectScript. Drag the other object that has the script attached into that slot, and it will link to that script.

Assets/Talker.js(2,21): BCE0018: The name ‘OtherObjectScript’ does not denote a valid type.

Well, I’m doing something wrong here.

“OtherObjectScript” needs to be replaced with the name of the script you want to reference.

This topic is actually covered by the Unity documentation:

Note that your own scripts are simply custom components and can be accessed the same way as the built-in components.

Yup. I was. Okay, I have this now:

Talker.JS

public var master : String = "test";
public var oScript: TListener; 

function Update () 
{
	Talker ("Yay") ;  
}

function  Talker (test1 : String) 
{
	oScript.TListener (test1);
}

and

TListener.JS

var slave : String;

function  TListener (test1 : String) 
{
	var test2 = test1;
	print(test2);
}

Now, I figured out how to drop the link from Cube2 onto the oScript entry in Inspector, and it works.

Now, How do I automate the designation of oScript, so that when I create objects in code they can be linked up too?

Or would the objects have to send their ‘pointer’ themselves to a global variable database, so it coul be accessed later?

1 Like

Ahhhhh, thank you! Your pointer was right on the mark:

With this, I can use the object NAME to find the part. Very useful in this case.

Thanks everyone. That got me started on my road to creating clustered AI groups to help me rule the world.

1 Like

Hello, I’ve used the following successfully when “OtherScript” is a .js Javascript script:

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

// Set foo DoSomething on the target variable assigned in the inspector.
var target : OtherScript;
function Update () {
// Set foo variable of the target object
target.foo = 2;
// Call do something on the target
target.DoSomething(“Hello”);
}

However, how do I do this when “OtherScript” is a .cs C# script?

When OtherScript is a C# script, I get:

The name ‘OtherScript’ does not denote a valid type.

Is there a work-around? Or, what am I doing wrong?

Thank you
Maximum respect,
GuitarGuy91403

Can anyone answer this guys question??? Wow it’s been since last year he asked. I was trying to look this up also

I am still looking for this :confused:

I’m still looking as well. I keep getting this error: NullReferenceException: Object reference not set to an instance of an object

Here’s my script trying to do what I believe is the same thing.

ballScript bs;
Scoring ns;

int ballScore;
int nodeScore;

public int totalScore;

// Use this for initialization
void Start () {

totalScore = 0;

}

// Update is called once per frame
void Update () {

ballScore = bs.score;
nodeScore = ns.score;

totalScore = ballScore + nodeScore;

print("Total Score = " + totalScore);

Super old thread but does this help

And do someone knows how it’s done in C#

Use UnityEvents. They let you set up a lot of communication in the Inspector view, which reduces the amount of code you need to maintain. JoeStrout put together a good tutorial video here.

Good!